bat文件
生成.bat
@echo off
:loop
data.exe //生成数据
right.exe //std
test.exe //测试数据
fc right.out test.out
if not errorlevel 1 goto loop
pause
goto loop
C++版详讲对拍
对拍用于验证算法正确性,不保证运行时间!!
一般有四个文件
一个对拍文件 dp.exe/dp.cpp
一个生成数据程序 data.exe/data.cpp
一个标准程序(一般会比较慢,但保证正确) std.exe/std.cpp
一个测试的程序 problem.exe/problem.cpp
下面以A+B问题为例,解释一下对拍代码
生成数据代码
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<iostream>
using namespace std;
int main(){
freopen("problem.in","w",stdout);//注意这里是输出,不是读入
srand(time(0));//根据时间置随机数 这样随机数周期长 近似认为是随机数 最大值为32767
//如果需要更大的数据可以
//rand()*rand()%DARA_MAX;
printf("%d %d\n",rand(),rand());
return 0;
}
标准程序代码//瞎写的qwq;
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
freopen("problem.in","r",stdin);
freopen("std.out","w",stdout);
int a,b;
cin>>a>>b;
while(b--) a++;
cout<<a;
return 0;
}
验证算法
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
freopen("problem.in","r",stdin);
freopen("problem.out","w",stdout);
int a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}
核心对拍代码
#include<cstdlib>
#include<iostream>
using namespace std;
int main(){
do{
system("data.exe");
system("std.exe");
system("problem.exe");
}while(!system("fc std.out problem.out"));
return 0;
}
所有文件如图所示
运行结果如图所示