对拍是什么呢?
对拍就是针对解决同一个问题写出两个不同程序,和一个随机数据生成器(用于生成测试输入)。先去用这个随机数据生成器的输出作为你这两个程序的输入,然后比较你这两个程序执行相同输入后的输出,可以找到一组使这两个程序输出不一样的数据(如果存在的话)。
怎么对拍呢?
首先,新建一个文件夹,在里面放入biaoda.exe,和test.exe,还有data.exe。
biaoda.exe是你暴力写的一个做法或者你从网上找的一份AC代码生成的程序,反正结果肯定是对的。
test.exe就是你的代码生成的程序,你不知道他对不对或者你知道他错了,但是你不知道哪里错了。
data.exe就是你的数据生成器 (用于生成测试输入) ,你可以用它去生成你认为的合法输入数据。
比如解决A+B问题的biaoda.cpp:
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
cout << a+b << endl;
return 0;
}
test.cpp:
#include <stdio.h>
int main(){
int a, b;
scanf("%d %d",&a, &b);
printf("%d\n", a+b);
return 0;
}
然后就是data.cpp,这个数据生成很简单,有些题目就没有这么容易了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <algorithm>
#include <windows.h>
using namespace std;
int main(){
srand(time(0));
//freopen("test0.in","w",stdout);
int a,b;
a=rand()%100+1,b=rand()%100+1;
printf("%d %d\n",a,b);
return 0;
}
根据这三个cpp生成三个exe,放入对拍文件夹里。然后对拍,
方法一(这个比方法二运行起来更快一点):
在该文件夹里新建一个 对拍bat文件,如 test.bat,然后运行。
:again
data > input.txt
biaoda < input.txt > biaoda_output.txt
test < input.txt > test_output.txt
fc biaoda_output.txt test_output.txt
if not errorlevel 1 goto again
pause
方法二:写一段cpp代码,对拍exe版:
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
//int t=200;
while(1)
{
//t--;
system("data.exe > data.txt");
system("biaoda.exe < data.txt > biaoda.txt");
system("test.exe < data.txt > test.txt");
if(system("fc test.txt biaoda.txt")) break;
}
if(t==0) cout<<"no error"<<endl;
else cout<<"error"<<endl;
//system("pause");
return 0;
}
根据这个,生成一个 对拍exe版.exe,然后这个exe也要放在对拍这个文件夹下,然后使用时运行这个exe就行。