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

文章目录

  • C/C++算法竞赛代码框架
    • 一、基本代码框架
      • 1.最简框架
      • 2.万能框架
    • 二、测试代码框架
      • 1.时间测试框架
      • 2.文件重定向框架
    • 三、本地测试框架
      • 1.本地测试框架代码:
      • 2.本地测试框架说明:
    • 四、终极框架
    • 附录

一、基本代码框架

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 LOCALfreopen("data.in", "r", stdin);   //提交代码时记得注释掉或删除freopen("data.out", "r", stdout); //提交代码时记得注释掉或删除
#endif/*code*/#ifdef LOCALprintf("\nTime used = %f", (double)clock() / CLOCKS_PER_SEC);
#endifreturn 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 LOCALfreopen("data.in", "r", stdin);   //提交代码时记得注释掉或删除freopen("data.out", "r", stdout); //提交代码时记得注释掉或删除
    #endif
    
    #ifdef LOCALprintf("\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 LOCALint main()
{gkd;
#ifdef LOCALfreopen("data.in", "r", stdin);   //提交代码时记得注释掉或删除freopen("data.out", "r", stdout); //提交代码时记得注释掉或删除
#endif/*code*/#ifdef LOCALprintf("\nTime used = %f", (double)clock() / CLOCKS_PER_SEC);
#endifreturn 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
    

C/C++算法竞赛代码框架相关推荐

  1. 算法竞赛训练指南代码仓库_数据仓库综合指南

    算法竞赛训练指南代码仓库 重点 (Top highlight) As a data scientist, it's valuable to have some idea of fundamental ...

  2. 算法竞赛中的输入输出框架

    算法竞赛中的输入输出框架 算法竞赛中的输入输出框架 标准输入输出 重定向 fopen 标准输入输出 #include <stdio.h> #define INF 10000000 /**输 ...

  3. 算法竞赛入门经典(刘汝佳)——代码笔记

    Reference: <算法竞赛入门经典>(刘汝佳)第一版.第二版 ------------------------------------------------------------ ...

  4. python3.4勾股定理代码_用于解答算法题目的Python3代码框架

    前言 最近在实习,任务并不是很重,就利用闲暇时间使用Python3在PAT网站上刷题,并致力于使用Python3的特性和函数式编程的理念,其中大部分题目都有着类似的输入输出格式,例如一行读入若干个数字 ...

  5. 算法竞赛——给定ATCG的DNA环状序列,求解最小表示字典序(附python代码及时间复杂度解析)

    题目:环状序列表示一般都会有很多种,比如一个环'CCTC',它的表示方法可能会有很多种,比如,CCTC,CTCC,TCCC,CCCT.这几种表示中,找出字典序最小的表示序列.(字符串只由A.T.G.C ...

  6. 《算法竞赛入门经典》(第二版)代码及详细解释(持续更新!)

    笔者中山大学硕士,医学生+计科学生的集合体,机器学习爱好者. 现发布[刘汝佳<算法竞赛入门经典>(第二版)--紫书]的例题和习题的代码和详细解释. 欢迎批评指正! 另外欢迎关注本人微信公众 ...

  7. UVA-814 邮件传输代理的交互 题解答案代码 算法竞赛入门经典第二版

    GitHub - jzplp/aoapc-UVA-Answer: 算法竞赛入门经典 例题和习题答案 刘汝佳 第二版 AC代码 #include<iostream> #include< ...

  8. UVA-12171 雕塑 题解答案代码 算法竞赛入门经典第二版

    GitHub - jzplp/aoapc-UVA-Answer: 算法竞赛入门经典 例题和习题答案 刘汝佳 第二版 这道题目在<算法竞赛入门经典第二版>书中标注了星号,也是第一道出现星号的 ...

  9. UVA-1598 交易所 题解答案代码 算法竞赛入门经典第二版

    GitHub - jzplp/aoapc-UVA-Answer: 算法竞赛入门经典 例题和习题答案 刘汝佳 第二版 AC代码 有意思的一个题目.书上说这是一个不错的优先队列练习题,但实际上它其实是一个 ...

最新文章

  1. Python可视化matplotlib绘制三维可视化图形(Three-Dimensional)
  2. Thread优先级之让步
  3. BZOJ 1503 treap
  4. linux ip addr peer,Linux网络IP设置
  5. 计算机应用乘法,计算机系统原理(十) 二进制整数的乘法运算和除法运算
  6. 稳坐开发领域霸主之位,揭秘C语言无可取代的几大原因!
  7. web 打印时 表格多页 导致缺少线段 看上去页面不完整的处理方案
  8. android小米便签源代码分析,小米开源便签Notes-源码研究(1)-导出功能整体思路
  9. 项目管理project模板_项目管理常用的10张图表推荐
  10. 火车票电子客票系统已全面上线,如何识别多种身份有效证件?
  11. EasyCaptcha图形验证码工具
  12. 计算机cpu天体图,电脑cpu天梯图2019|最新Intel/AMD处理器性能排行2019
  13. 最小函数依赖集,候选码,保持3NF依赖性的分解例题
  14. 人工神经网络指标是什么,人工神经网络指标分析
  15. 微信支付-同一个订单多次请求(生成二维码)方案
  16. 海外自媒体多账号运营注意事项看这里!
  17. html input hiden,input hidden属性
  18. Unity报错:Assertion failed on expression: ‘IsMatrixValid(matrix)‘...的解决办法
  19. 【算法-剑指 Offer】62. 圆圈中最后剩下的数字(环形链表;约瑟夫环;动态规划)
  20. javaweb-jsp(上)

热门文章

  1. TensorFlow之Vscode调试
  2. web之Attribute
  3. opencv安装教程python3.7_Python3.7中安装openCV库的方法
  4. faster rcnn fpn_Faster-RCNN详解和torchvision源码解读(三):特征提取
  5. 每日三道前端面试题--vue 第三弹
  6. python闭包和函数调用区别_python – 函数闭包与可调用类
  7. CESIUM加载glb的模型
  8. linux rewind函数,C语言rewind()函数:将文件指针重新指向文件开头
  9. python语言的记事本在哪_用python语言编写一个简单记事本
  10. python爬虫下载模块_python爬虫模块之HTML下载模块