对拍
这里先写一种用c++写的对拍程序,其他的以后再补;
一.C++
一共有四个 .exe
用于生成测试数据的 data.exe
正确代码生成的 biaoda.exe
要测试代码生成的 test.exe
以及调用这三者 对拍启动器.exe
!!注意:这三者要放在同一个目录下!!
(我写的程序要在前三个里加上 将数据从文件读入 输出到文件 的语句;相信这对oier都很熟悉了)
1.用于生成测试数据的 data.exe
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
freopen("data.txt","w",stdout); //重新定义输出对象,输出到data.txt
srand(time(0));//这里是设置随机数函数,得到的随机数随时间变化
srand( (unsigned)time( NULL ) );
int a,b,t,n,k,q;//后面的部分根据题目自己设置,这里为HDU-3577对应的随机数据
t=rand()%10+1;
printf("%d\n",t);
for(int i=1;i<=t;i++)
{
k=rand()%1000+1;
q=rand()%100000+1;
printf("%d %d\n",k,q);
for(int j=1;j<=q;j++)
{
do
{
a=rand()%1000000+1;
b=rand()%1000000+1;
}
while(a>=b);
printf("%d %d\n",a,b);
}
}
return 0;
}
同理 test.exe 与 biaoda.exe 都要加上 freopen(); (当然也可以用fopen()之类的);
为了方便给出代码;
2.在 test.exe 代码的 main 函数开头加上
freopen("data.txt","r",stdin);
freopen("test.txt","w",stdout);
3.在 biaoda.exe 代码的相同位置加上
freopen("data.txt","r",stdin);
freopen("biaoda.txt","w",stdout);
4.调用并对比结果的 对拍启动器.exe
#include<cstdio>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
int t=0,begintime,endtime;
while(t<=100000)
{
t++;
printf("NO.%d \n",t);
system("data.exe");//运行data.exe
begintime=clock();
system("biaoda.exe");//运行bioda.exe
endtime=clock();
printf("biaoda:运行时间:%d\n",endtime-begintime);
begintime=clock();
system("test.exe");//运行test.exe
endtime=clock();
printf("test:运行时间:%d\n",endtime-begintime);
if(system("fc test.txt biaoda.txt")) system("pause");//对比结果
}
system("pause");
return 0;
}
相信相比较其他操作,这样的方式对于选手们更容易理解吧。