C++对拍程序写法(转)

来源:互联网 发布:java前端面试题 编辑:程序博客网 时间:2024/06/12 00:22

http://blog.csdn.net/code12hour/article/details/51252457

对拍是什么呢?

对拍就是你给两个程序,和一个随机数据生成器,然后系统去用这个随机数据生成器的输出作为你这两个程序的输入,然后比较你这两个程序的输出,可以找到一组使这两个程序输出不一样的数据(如果存在的话)

怎么对拍呢?

首先,新建一个文件夹,

然后,在里面放入biaoda.exe,和test.exe,还有data.exe

biaoda.exe是你暴力写的一个做法或者你从网上找的一份AC代码生成的程序,反正结果肯定是对的。

test.exe就是你的代码生成的程序,你不知道他对不对或者你知道他是WA的但是你不知道哪里WA了,

data.exe就是你的数据生成器,你可以用它去生成你认为的合法数据

比如

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;  //ofstream cout("data.in");  int main()  {      srand(time(0));  //  srand( (unsigned)time( NULL ) );      //freopen("input.txt","w",stdout);       int a,b;      a=rand()%100+1,b=rand()%100+1;      printf("%d %d\n",a,b);       return 0;  } 

然后你根据这三个个cpp生成三个个exe,然后放入那个对拍文件夹里

然后,

方法一(这个比方法二运行起来更快一点):

新建一个 对拍bat版.txt

输入:

: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 

把扩展名txt改为bat,当然要保证这个bat文件也在对拍文件夹下,使用时运行这个bat就可以

方法二:

这是一段cpp代码,对拍exe版.cpp:

#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就行

当然,假如你有dev-cpp这个编译器的话,就很快,因为它可以直接编译cpp文件或者c文件,然后在同一个文件夹下生成对应的exe文件,然后这样就很方便啦,希望能对大家有所帮助

end