对拍是各种计算机考试检查时必备工具,实际上十分强大,只要你的暴力没有写错就没有问题。
对拍的意思:(怎么有点语文课的意思雾)
对:看见‘对’就可以知道有两个。
拍:就是把两个程序结果拍在一起,对照(有点牵强)。
实践
由于Windows和Linux系统不同,平常大多数人都用Windows,而在noi系列赛事中都采用Linux系统。
Windows
就以a+b为例吧。
首先摆一个可能是正解的东西。
这个代码文件名:1.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include<cstdio> #include<windows.h> #include<ctime> #include<cstdlib> using namespace std; int main() { int a,b; scanf("%d%d",&a,&b); for(int i=1;i<=n;i++)a++,b--; printf("%d\n",a+b); return 0; }
|
现在摆一个一定是正确的但时间和内存超标的代码,当然例子没有超标。
这个代码文件名:2.cpp
1 2 3 4 5 6 7 8 9
| #include<cstdio> using namespace std; int main() { int a,b; scanf("%d%d",&a,&b); printf("%d\n",a+b); return 0; }
|
构造数据。
注意
不能只用rand构造,因为rand表示伪随机数,在一定基数下一定,所以用rand构造的随机数出题人也知道你构造的是什么(然后故意卡你)
所以介绍一个新东西。
这个怎么用???
这个时候,他的随机数随时间变化(笑),出题人再也搞不出来卡你的算法了;
rand要用cstdlib库
代码文件名:data.cpp
1 2 3 4 5 6 7 8 9
| #include<cstdio> #include<ctime> #include<cstdlib> using namespace std; int main() { srand(time(NULL)); printf("%d %d\n",rand()%1000,rand()%1000); }
|
注意换行符必须加,否则你想想手动输入数据不加换行会怎么样。
下面的就是对拍程序:
代码文件名:duipai.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include<stdio.h> #include<time.h> #include<windows.h> using namespace std; int main() { int t=1000; while(t!=0) { clock_t t1,t2; system("data.exe>data.txt"); t1=clock(); system("1.exe<data.txt>1.txt"); t2=clock()-t1; system("2.exe<data.txt>2.txt"); printf("time=%dms\n",t2); if(system("fc 1.txt 2.txt"))break; t--; } if(t==0)printf("no error\n"); else printf("error\n"); getchar(); return 0; }
|
程序运行。

如果程序出错就在生成的txt文件里找样例:
data.txt里是样例。1.txt和2.txt是输出。

Linux系统
这个系统是我不经常接触的(实际上只在考试接触),所以如有口胡请见谅,请指出。
还是a+b。(其实Windows也可以这么搞)
文件名:1.cpp
1 2 3 4 5 6 7 8 9 10 11 12
| #include<cstdio> using namespace std; int main() { freopen("add.in","r",stdin); freopen("add.out","w",stdout); int a,b; scanf("%d%d",&a,&b); for(int i=1;i<=n;i++)a++,b--; printf("%d\n",a+b); return 0; }
|
暴力:
文件名:2.cpp
1 2 3 4 5 6 7 8 9 10 11
| #include<cstdio> using namespace std; int main() { freopen("add.in","r",stdin); freopen("add1.out","w",stdout); int a,b; scanf("%d%d",&a,&b); printf("%d\n",a+b); return 0; }
|
造数据:
文件名:data.cpp
1 2 3 4 5 6 7 8 9 10
| #include<cstdio> #include<ctime> #include<cstdlib> using namespace std; int main() { freopen("add.in","w",stdout); srand(time(NULL)); printf("%d %d\n",rand()%1000,rand()%1000); }
|
对拍:
文件名:duipai.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include<bits/stdc++.h> using namespace std; int main() { int t=10; while(t!=0) { clock_t t1,t2; system("./data"); t1=clock(); system("./1"); t2=clock()-t1; system("./2"); printf("time=%dms\n",t2); if(system("diff add.out add1.out"))break; t--; } if(t==0)printf("no error\n"); else printf("error\n"); getchar(); return 0; }
|
由于不善于用linux系统,所以没有留下图片,抱歉