对拍的形式就是就是通过 与暴力算法 去验证自己的算法的可行性,是否存在边界问题没有考虑,找出 Hack 数据
对拍
1. 文件结构
duipai/├── gen.cpp├── std.cpp├── solve.cpp└── duipai.cpp2. 数据生成器
2.1 生成整数
#include <bits/stdc++.h>using namespace std;
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng);}
int main() { int n = rnd(1, 10);
cout << n << endl;
for (int i = 1; i <= n; i++) { cout << rnd(-100, 100) << " "; }
cout << endl;
return 0;}2.2 生成浮点数
#include <bits/stdc++.h>using namespace std;
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
double rnd(double l, double r) { return uniform_real_distribution<double>(l, r)(rng);}
int main() { int n = 10;
cout << n << endl;
cout << fixed << setprecision(6);
for (int i = 1; i <= n; i++) { cout << rnd(-100.0, 100.0) << " "; }
cout << endl;
return 0;}2.3 同时生成整数和浮点数
#include <bits/stdc++.h>using namespace std;
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
int rndInt(int l, int r) { return uniform_int_distribution<int>(l, r)(rng);}
double rndDouble(double l, double r) { return uniform_real_distribution<double>(l, r)(rng);}
int main() { int n = rndInt(1, 10);
cout << n << endl;
for (int i = 1; i <= n; i++) { cout << rndInt(-100, 100) << " "; }
cout << endl;
cout << fixed << setprecision(6);
for (int i = 1; i <= n; i++) { cout << rndDouble(-100.0, 100.0) << " "; }
cout << endl;
return 0;}3. Windows 对拍
3.1 编译
g++ gen.cpp -std=c++17 -O2 -o gen.exeg++ std.cpp -std=c++17 -O2 -o std.exeg++ solve.cpp -std=c++17 -O2 -o solve.exeg++ duipai.cpp -std=c++17 -O2 -o duipai.exe3.2 duipai.cpp
#include <bits/stdc++.h>using namespace std;
int main() { int cnt = 0;
while (true) { cnt++;
system("gen.exe > test.in");
system("std.exe < test.in > std.out"); system("solve.exe < test.in > solve.out");
if (system("fc std.out solve.out > nul")) { cout << "\nWrong Answer!" << endl; cout << "Test: " << cnt << endl;
cout << "\nInput:" << endl; system("type test.in");
cout << "\nStandard Output:" << endl; system("type std.out");
cout << "\nYour Output:" << endl; system("type solve.out");
break; }
cout << "Accepted: " << cnt << "\r"; }
return 0;}运行:
duipai.exe4. Linux 对拍
4.1 编译
g++ gen.cpp -std=c++17 -O2 -o geng++ std.cpp -std=c++17 -O2 -o stdg++ solve.cpp -std=c++17 -O2 -o solveg++ duipai.cpp -std=c++17 -O2 -o duipai4.2 duipai.cpp
#include <bits/stdc++.h>using namespace std;
int main() { int cnt = 0;
while (true) { cnt++;
system("./gen > test.in");
system("./std < test.in > std.out"); system("./solve < test.in > solve.out");
if (system("diff std.out solve.out > /dev/null")) { cout << "\nWrong Answer!" << endl; cout << "Test: " << cnt << endl;
cout << "\nInput:" << endl; system("cat test.in");
cout << "\nStandard Output:" << endl; system("cat std.out");
cout << "\nYour Output:" << endl; system("cat solve.out");
break; }
cout << "Accepted: " << cnt << "\r"; }
return 0;}运行:
./duipai5. 常用命令
Windows
gen.exe > test.instd.exe < test.in > std.outsolve.exe < test.in > solve.outfc std.out solve.outLinux
./gen > test.in./std < test.in > std.out./solve < test.in > solve.outdiff std.out solve.out6. 浮点数对拍
浮点数不要直接比较文件,使用误差判断:
double a, b;
if (fabs(a - b) > 1e-6) { cout << "Wrong Answer" << endl;}常用:
bool equal(double a, double b) { return fabs(a - b) <= 1e-6;}