本蒟蒻在zxp大佬的指導下學習了對拍html
好像沒什麼好記的, 那就走下形式, 放一下模板吧!c++
對拍要有四個程序算法
分別是函數
- rd(求隨機數據的)
- std(用 萬能算法暴力 求出真確答案)
- my(用本身的程序求出答案)
- compare(比較std和my)
#include<bits/stdc++.h> using namespace std; const int N = 1e5+7; int rd(int L,int R) { return rand()%(R-L+1) + L; } int a[N]; int main() { srand((unsigned)time(0)); int n = 1e4; for(int i=1;i<=n;++i) a[i] = rd(-1e5,1e5); printf("%d\n",n); for(int i=1;i<=n;++i) printf("%d ",a[i]); cout << endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int N = 1e5+7; int a[N],sum[N]; signed main() { int n = read(); for(int i=1;i<=n;++i) a[i] = read(); for(int i=1;i<=n;++i) sum[i] = sum[i-1] + a[i]; int ans = -INF; for(int l=1;l<=n;++l) { for(int r=l;r<=n;++r) { int s = sum[r] - sum[l-1]; ans = max(ans, s); } } printf("%lld\n",ans); return 0; }
#include<bits/stdc++.h> using namespace std; int N = 2e5+7; int n; int a[N]; signed main() { n = read(); for(int i=1;i<=n;++i) a[i] = read(); int ans = -INF, cnt = 0, res = 0; for(int i=1;i<=n;++i) { res += a[i]; ++cnt; if(cnt > 0) ans = max(ans, res); if(res < 0) res = 0, cnt = 0; } printf("%lld\n",ans); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { while(true) { system("rd.exe > rd.txt"); //把 rd.exe得出的隨機數載入rd.txt system("my.exe < rd.txt > x.txt"); system("std.exe < rd.txt > y.txt"); //把使用 rd.txt 爲輸入數據的 my.exe 的輸出結果載入x.txt //把使用 rd.txt 爲輸入數據的 std.exe 的輸出結果載入x.txt if(system("fc x.txt y.txt")) break;//若x.txt和y.txt相同,就返回0 } return 0; }
以上就是所有內容,相信衆爲訪客可輕易理解...學習
如有不妥之處,請私信我,我儘可能及時改正。spa
如有錯誤之處,請私聊 zxp大佬,我還會改正。code