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 10000
const int MAX = 1000000000;
/*大規模陣列的定義*/
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 10000 const int MAX = 1000000000;宏定義和
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宏定義注釋掉即可,
附錄
- 萬能頭檔案的定義
#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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/260364.html
標籤:其他
