- 分享
对拍
- @ 2026-10-3 8:37:56
写下你的对拍文档,越详细越好,争取新生小登也能看得懂
3 条评论
-
-
经过了生米cz的提问,我猛然惊醒还有对拍这种东西但我不会,故学习后来发一篇
情景模拟
这东西原理很简单,你学了个算法,但你不知道你到底会不会用,你有一个学霸朋友,信息老师负责出题,出数据老师根据题目造数据,改卷老师把同一组数据同时发给你的程序和学霸的程序,看结果是否一样。
注意到上面有许多人,在对拍中就是: 信息老师:题目
你:你的代码
学霸:那个永远不会错的代码(常用暴力代码)
出数据老师:出数据的代码
改卷老师:把你和学霸的代码 结果进行比对,检查是否有不一样的并输出结果
(肥肠好懂啊)
具体做法:
新建一个文件夹 里面放3个代码 bf.cpp 暴力代码 sol.cpp 你要检查的代码 gen.cpp 出数据 还有一个 pai.cpp 负责比较输出
注意
三个代码一定要先运行一下,保证文件夹里有了bf.exe sol.exe gen.exe
具体代码
pai.cpp永远不会变,建议复制一下(
我用的大肥鱼deepseek的,懒得自己打)建议学习一下别人的还是,我的这个只做参考点击查看代码
#include <bits/stdc++.h> #include <windows.h> using namespace std; string readFile(const string& filename) { ifstream fin(filename); stringstream ss; ss << fin.rdbuf(); return ss.str(); } long long getTimeMs() { return GetTickCount64(); } int main() { int cnt = 0; int MAX_TEST = 1000; while (cnt < MAX_TEST) { cnt++; system("gen.exe > data.in"); long long t1 = getTimeMs(); system("sol.exe < data.in > sol.out"); long long t2 = getTimeMs(); system("bf.exe < data.in > bf.out"); string data = readFile("data.in"); string solOut = readFile("sol.out"); string bfOut = readFile("bf.out"); cout << "============================================\n"; cout << "第 " << cnt << " 组数据\n"; cout << "--------------------------------------------\n"; cout << "[输入数据]\n" << data; cout << "--------------------------------------------\n"; cout << "[正确结果 bf]\n" << bfOut; cout << "--------------------------------------------\n"; cout << "[你的结果 sol]\n" << solOut; cout << "--------------------------------------------\n"; cout << "[sol 运行时间] " << (t2 - t1) << " ms\n"; cout << "============================================\n"; // 5. 比较 if (solOut != bfOut) { cout << "\n*** WA!结果不一致 ***\n"; cout << "出错数据已保存在 data.in\n"; cout << "你的输出在 sol.out,正确答案在 bf.out\n\n"; break; } else { cout << "AC\n\n"; } Sleep(1000); } cout << "对拍结束,按任意键退出...\n"; system("pause"); return 0; }
接下来的就要你自己根据题目自己改了 以a+b problem为例
sol.cpp: (要检查的代码)
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; cout << a + b << endl; return 0; }gen.cpp: (出数据)
#include <bits/stdc++.h> using namespace std; int main() { srand(time(0)); int a = rand() % 100 + 1; int b = rand() % 100 + 1; cout << a << " " << b << endl; return 0; }bf.cpp: (暴力)
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int sum = 0; for (int i = 1; i <= a; i++) sum++; for (int i = 1; i <= b; i++) sum++; cout << sum << endl; return 0; }喵喵喵,以后改好这三个代码直接运行pai.exe就行
最后可用的目录是这样的:
- bf.cpp - bf.exe - bf.out - data.in - gen.cpp - gen.exe - pai.cpp - pai.exe - sol.cpp - sol.exe - sol.out用完一次后,下一次直接运行pai.exe即可
最后感叹一句ai真好用,建议结合ai来教你搞这玩意,哪里报错或不知道下一步该怎么做直接把信息截屏发给ai
(
深入浅出有教啊)
修改日志
2026年10月3日09:34:46 修正了笔误和表述不清
🤣 2👎 2 -
@ 2026-10-3 8:58:56
#include<bits/stdc++.h> using namespace std; int T=1000;//数据组数 int main(){ while(T--){ //data.exe是数据生成器 //1.exe是正解/暴力 //2.exe是要被拍的代码 system("data.exe > data.txt");//生成数据 system("1.exe < data.txt > 1.txt");//把数据导入1.txt,生成正确输出 system("2.exe < data.txt > 2.txt");//把数据导入2.txt,生成可能错误的输出 if(system("fc /N /W 2.txt 1.txt ")){//利用FC对比两份输出,若不同进入if cout<<"WA!!!\n"; cout<<"数据:\n"; system("type data.txt"); cout<<"正确:\n"; system("type 1.txt"); cout<<"\n"; cout<<"错误:\n"; system("type 2.txt"); cout<<"\n"; return 0; //输出使两份代码输出不同的数据,并输出正确的输出和错误的输出,结束对拍 } } cout<<T<<"/"<<T<<"\n"; cout<<"AC!!!"; return 0; } -
#include <bits/stdc++.h> #include <windows.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int Case=0;//这个是记录拍到了第几组 while (true) { Case++; cout<<"Test Case #"<<Case<<endl;//输出现在是第几组 //目前可以说,以上都是装饰(无实际作用) //这里我们假定我们写好了暴力和正解,你想对拍 //首先你需要写一个数据生成器 data.cpp //然后将本对拍代码、正解代码、暴力代码、数据生成器代码扔到同一个文件夹里 //接着将正解代码、暴力代码、数据生成器代码全部编译一遍 //最后运行对拍代码 // 1. 生成数据(data.exe 输出到 data.txt) system("data.exe > data.txt"); // cout<<"Generated data."<<endl; // 2. 运行暴力解法(baoli.exe 从 data.txt 读入,输出到 baoli.txt) system("暴力.exe < data.txt > baoli.txt"); // cout << "Ran brute-force solution." << endl; // 3. 运行正解(std.exe 从 data.txt 读入,输出到 std.txt) system("正解.exe < data.txt > std.txt"); // cout << "Ran correct solution." << endl; // 4. 比较输出(使用 fc /N 显示行号,/W 忽略空白差异) //注意到你其实可以不用忽略空白差异,否则可以问 zifeiwoye 发生了什么事 if(system("fc /N /W baoli.txt std.txt > nul")){//这个返回 1 的前提是两个文件不同 cout<<"WA on test case #"<<Case<<"!"<<endl; system("fc /N /W baoli.txt std.txt"); // 显示具体差异 system("pause"); break; } else { cout <<"AC!"<<endl; } } return 0; }👀 3
- 1