C/C++算法竞赛代码框架

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
    
  • 34
    点赞
  • 93
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 20
    评论
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BkbK-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值