对拍
Nemlit
2018-03-02 14:49:19
# 何为对拍?
### 对拍是什么?两个人对着互相拍对方?
### 显然并不是。。。
### 很多时候,我们或许有一个标程(反正答案是对的),
### 自己的程序,一组一组的数据经过我们的手动输入后并没有什么问题,可就是有错……
### 咋办?一个一个输数据得到什么时候啊,
### 于是,一个很“高端”的东西诞生了————对拍。
### 对拍简单说就是把两个程序对于同一个输入的输出进行比较
### 没有问题就再来,有问题就停下来,
### 你就可以找到你程序的问题所在了
```cpp
```
# 为何对拍?
### 我认为对拍在考试的时候是很有用的,
### 比如2017年noip提高组复赛第一题,
### std是(a*b-a-b),
### 但是这么古怪的题目解法就算想到了也不一定会信。
### 这个时候可以写一份暴力代码来进行对拍。
```cpp
```
# 如何对拍?
### 首先std.cpp里放你自己的代码,编译
### test.cpp里放标程或者是你能保证答案正确可是TLE的代码,编译
### 然后自己写一个rand.cpp
### 按照题目格式输出一组数据
### 例如题目是求出1~n的和,我们就会有两种算法:
### test.cpp里的代码是:
```cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("data.out","r",stdin);
freopen("test.out","w",stdout);
int i,n;
long int sum = 0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum+=i;
}
printf("%d\n",sum);
return 0;
}
```
### 而std.cpp里的代码是:
```
#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("data.out","r",stdin);
freopen("std.out","w",stdout);
int n;
long long sum = 0;
scanf("%d",&n);
sum=n*(n+1)/2;
printf("%d\n",sum);
return 0;
}
```
### 那你rand.cpp就这么写:
```
#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("data.out","w",stdout);
srand(time(NULL));
int a=rand()%10000+1;
printf("%d\n",a);
return 0;
}
```
### 然后编译成rand.exe
### 最后运行 对拍程序 等待结果即可,如果拍不上程序会自动暂停下来,这个时候就可以开始写下一题了~~~
### 对拍.cpp 的代码如下:
```
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long i=0;
while(1)
{
printf("The result of No. %d Case is: ",++i);
system("data.exe > data.txt");
system("std.exe < data.txt > std.txt");
system("test.exe < data.txt > test.txt");
if (system("diff std.out test.out"))//比较程序的输出
{
puts("WA");
return 0;
}
else puts("AC");
}
return 0;
}
```
### 如果你还想看看自己的算法快了多少,对拍.cpp 可以这么写:
```
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long i=0,s,t;
while(1)
{
printf("The result of No. %d Case is: ",++i);
system("data.exe > data.txt");
s=clock();//测试时间
system("std.exe < data.txt > std.txt");
t=clock();
system("test.exe < data.txt > test.txt");
if(system("diff std.out test.out"))
{
puts("WA");
return 0;
}
else puts("AC");
printf("Save %d millisecond\n",t-s);
}
return 0;
}
```
### 如果是在Ubantu下运行对拍,代码如下:
```
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i=0;
while(1)
{
system("./make");
system("./gcd");
system("./gcdBL");
if(system("diff gcd.out gcdBL.out -w")) return puts("WA"),0;
else printf("Is %d : AC\n",++i);
}
}
```