程序对拍是在编程复赛或平时练习解题方法时的重要策略。随机数据将自己的代码和标准代码做对比,比较差异。就拿排序代码举例:
随机生成代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
freopen("cs.in","r",stdin);//文件,很重要,为了让其他程序读入,但这里的读入文件不需要
freopen("cs.in","w",stdout);
srand(time(0));//用srand生成随机数,切记要加“time(0)”!
int n=5+rand()%1000;//这里是将n随机赋值为5~1000的数
cout<<n<<endl;//存入文件
while(n--){
int a=1+rand()%800;//随机存入数字,就是排序时要排的数字
cout<<a<<' ';//输出随机数字
}
return 0;
}
这里要比较的代码我就不展示了,如果有需要,点击—>传送门
对拍代码:
#include<stdio.h>
#include<cstdlib>//或者是“#include<windows.h>”
#include<ctime>//比较时间的代码,可以不需要,但不能用"clock();"
#include<iostream>
using namespace std;
int main(){
double a,b,c,d;
for(int T=1;T<=100;T++){//循环次数
system("px.exe");//运行程序
d=clock();//记录时间差,因为这里记录的是全部运行时间
system("pxx.exe");//同上
b=clock();//记录运行耗时
c=clock();//时间差
system("pai.exe");//同上
a=clock();//记录运行时间
if(system("fc pxx.ans pxx.out")){//“fc”为判断差异,".ans"和".out"是比较代码,如果不同说明代码有问题
cout<<"Wrong Answer!"<<endl;
}
else{
printf("Accepted question #%d,used Your:%.0lfms Computer:%.0lfms\n",T,a-c,b-d);//通过,输出时间
}
system("pause");//按任意键继续…………,可以注释掉
cout<<endl<<"___________________"<<endl<<endl;//分割开来,方便看
}
return 0;
}
制作不易,留个赞再走呗!