C/C++算法竞赛代码框架
文章目录
一、基本代码框架
1.最简框架
最初接触C/C++时,没有学习文件、函数等知识,仅知道在这个框架下写出语句就可以在终端进行基本输入输出。
-
(1)C语言
#include <stdio.h> int main() { /*code*/ return 0; }
-
(2)C++
#include <iostream> using namespace std; int main() { /*code*/ return 0; }
2.万能框架
随着学习的深入,基本的输入输出已经无法满足做题的需要,可以为了方便程序编写,事先将常用头文件包含进来。
-
C语言(包含常用头文件)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main() { /*code*/ return 0; }
-
C++(包含万能头文件)
#include <bits/stdc++.h> using namespace std; int main() { /*code*/ return 0; }
二、测试代码框架
1.时间测试框架
在面临较大规模的数据输入时,需要大致判断程序运行时间和算法效率的对比时,可以使用<time.h>
头文件,并在输出的最后一行打印出程序的时间。clock()
函数获得程序运行的时间,CLOCKS_PER_SEC
和系统相关,两者相除便是程序运行秒数。由于输入数据会占用程序运行时间,可以使用文件重定向方法(见下文)和“管道”小技巧:
管道小技巧:
使用命令行运行程序echo 数据 | 程序名
-
C语言
#include <stdio.h> #include <time.h> int main() { /*code*/ printf("\nTime used = %f", (double)clock() / CLOCKS_PER_SEC); return 0; }
-
C++
#include <bits/stdc++.h> using namespace std; int main() { /*code*/ printf("\nTime used = %f", (double)clock() / CLOCKS_PER_SEC); return 0; }
2.文件重定向框架
对于大规模数据的输入和输出,可以将标准输入输出重定向到程序同一目录下的输入data.in
输出data.out
文件中。使用重定向语句:freopen( "data.in", "r", stdin);
和freopen( "data.out", "r", stdout);
。在提交代码时一定记得将这两行语句注释掉!!!
-
C语言
#include <stdio.h> int main() { freopen("data.in", "r", stdin); //提交代码时记得注释掉或删除 freopen("data.out", "r", stdout); //提交代码时记得注释掉或删除 /*code*/ return 0; }
-
C++
#include <bits/stdc++.h> using namespace std; int main() { freopen("data.in", "r", stdin); //提交代码时记得注释掉或删除 freopen("data.out", "r", stdout); //提交代码时记得注释掉或删除 /*code*/ return 0; }
三、本地测试框架
1.本地测试框架代码:
C++可以兼容C语言程序,且拥有更多函数模板、类模板和算法,因此终极框架采取C++语言。
#define LOCAL //本地调试宏定义
#include <bits/stdc++.h> //万能头文件
/*宏定义及常量定义部分*/
#define INF 10000000
const int N = 10;
/*大规模数组的定义*/
int Array[INF];
using namespace std;
int main()
{
#ifdef LOCAL
freopen("data.in", "r", stdin); //提交代码时记得注释掉或删除
freopen("data.out", "r", stdout); //提交代码时记得注释掉或删除
#endif
/*code*/
#ifdef LOCAL
printf("\nTime used = %f", (double)clock() / CLOCKS_PER_SEC);
#endif
return 0;
}
2.本地测试框架说明:
-
1.测试本地条件编译宏定义
#define LOCAL
定义宏用于本地测试时的条件编译,提交代码时仅需将此行注释掉
-
2.万能头文件
#include <bits/stdc++.h> //万能头文件
C++的万能头文件,此头文件包含了几乎所有的头文件,具体头文件内容定义见本文末附录部分。大部分竞赛和oj平台支持万能头文件,但不推荐在工程上使用。
-
3.常量及宏定义
/*宏定义及常量定义部分*/ #define INF 100000000 const int N = 10;
宏定义和
const
常量定义均可以用来定义常量,方便更改常量的值,且提高代码可读性,在存储空间充足的条件下,推荐const
常量定义,可以进行类型检查。 -
4.大规模数组定义
int Array[INF];
将大规模数组定义在
main
函数外,定义成全局变量,一是可以无需手动初始化,全局变量数组定义后自动初始化;二是全局变量存储在数据区,减小栈区的开销。 -
5.条件编译文件重定向语句及运行时间测试语句
#ifdef LOCAL freopen("data.in", "r", stdin); //提交代码时记得注释掉或删除 freopen("data.out", "r", stdout); //提交代码时记得注释掉或删除 #endif
#ifdef LOCAL printf("\nTime used = %f", (double)clock() / CLOCKS_PER_SEC); #endif
将重定向语句及运行时间测试语句均设置为条件编译,在本地编译运行时可以重定向输入输出并查看运行时间,提交代码时,只需要将第一行的
LOCAL
宏定义注释掉即可。
四、终极框架
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
#define mst(x,i) memset(x,i,sizeof(x))
#define gkd ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lb(x) (x&-x)
const int maxm=9+1e6;
const int N=9;
const int maxn=9+2e3;
#define LOCAL
int main()
{
gkd;
#ifdef LOCAL
freopen("data.in", "r", stdin); //提交代码时记得注释掉或删除
freopen("data.out", "r", stdout); //提交代码时记得注释掉或删除
#endif
/*code*/
#ifdef LOCAL
printf("\nTime used = %f", (double)clock() / CLOCKS_PER_SEC);
#endif
return 0;
}
附录
- 万能头文件的定义
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L #include <ccomplex> #include <cfenv> #include <cinttypes> #include <cstdalign> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif // C++ #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <typeindex> #include <type_traits> #include <unordered_map> #include <unordered_set> #endif