学习对拍程序( ̄▽ ̄)"

  打比赛的时候不太喜欢造数据。往往写好了之后一测样例,对了!样例对了就是对了,交!   -----WA的一声就哭出来了┭┮﹏┭┮,于是开始找错误数据,找bug。。。

  今天小白我学习了对拍程序,以后一些题目就可以用这个啦嘻嘻。

  学习到两种方法,一种是以前比较熟悉的重定向到文件,不过那样可能会忘记删去重定向直接交白白wa一发;  另一种是批处理文件,看起来好像比较高级的样子,下面两种都端上来介绍一下。

  一、重定向到文件

  首先写出暴力的代码baoli.cpp (答案一定是对的)和自己的代码youxiu.cpp

  baoli.cpp重定向      freopen("data.in","r",stdin);
                              freopen("baoli.out","w",stdout);

 1 #include <iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 
 5 int main(int argc, char** argv) {
 6     freopen("data.in","r",stdin);
 7     freopen("baoli.out","w",stdout);
 8     int n;
 9     cin>>n;
10     int ans=0;
11     for(int i=1;i<=n;i++) ans+=i;
12     cout<<ans<<endl;
13     return 0;
14 }
View Code

  youxiu.cpp重定向     freopen("data.in","r",stdin);
                              freopen("youxiu.out","w",stdout);

 1 #include <iostream>
 2 #include<cstdio> 
 3 using namespace std;
 4 
 5 int main(int argc, char** argv) {
 6     freopen("data.in","r",stdin);
 7     freopen("youxiu.out","w",stdout);
 8     int n;
 9     cin>>n;
10     cout<<n*(n+1)/2<<endl;
11     return 0;
12 }
View Code

  写一个生成数据的 rand.cpp

 1 #include <iostream>
 2 #include<cstdlib>
 3 #include<ctime>
 4 #include<sstream>
 5 #define randint(l,r) ((l)+rand()%((r)-(l)+1))
 6 using namespace std;
 7 int main(int argc, char** argv) {
 8     int seed=time(NULL);
 9     if(argc>1){  //如果有参数
10         stringstream ss;
11         ss.clear();
12         ss << argv[1];
13         ss >> seed;  //把参数转换成整数赋值给seed
14     }
15     srand(seed);
16     //以上为随机数初始化,请勿修改
17     freopen("data.in","w",stdout);
18     int n;
19      n = 1 + rand()%10000;  //随机生成一个1到10000的自然数
20     cout<<n<<endl;
21     return 0;
22 }
View Code

  再写一个对拍的程序,然后运行这个就行了  (之前的.cpp要保存并运行)

 1 #include<cstdio>
 2 #include<iostream>#define File(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
 3 using namespace std;
 4 
 5 inline int read();
 6 int main()
 7 {
 8     int s=0;
 9     while(1)
10     {
11         system("rand.exe");
12         system("youxiu.exe");
13         system("baoli.exe");
14         cout<<"here"<<endl;
15         if(system("fc youxiu.out baoli.out"))
16             system("pause");
17         else
18             cout<<++s<<endl;
19     }
20     return 0;
21 }
View Code

  参考了一篇博客,看不懂还可以看这里:https://www.cnblogs.com/SeanOcean/p/10975527.html

二、批处理文件

  这个就更简单啦。

  写好暴力代码baoli.cpp 和自己的代码youxiu.cpp ,不用加重定向,正常写。

  写生成数据文件。

 1 #include <iostream>
 2 #include<cstdlib>
 3 #include<ctime>
 4 #include<sstream>
 5 #define randint(l,r) ((l)+rand()%((r)-(l)+1))
 6 using namespace std;
 7 int main(int argc, char** argv) {
 8     int seed=time(NULL);
 9     if(argc>1){  //如果有参数
10         std::stringstream ss;
11         ss.clear();
12         ss << argv[1];
13         ss >> seed;
14     }
15     srand(seed);  //把参数转换成整数赋值给seed
16     //以上为随机数初始化,请勿修改
17     //一下生成自己的随机数 
18     int n;
19     n = 1 + rand()%10000;  //随机生成一个1到10000的自然数
20     cout<<n<<endl;
21     return 0;
22 }
View Code

  然后写一个批处理文件。怎么新建呢?直接新建txt文本文件把后缀改为.bat就行了。右键编辑,在里面写。

  直接上代码:

1 @echo off
2 :loop
3     rand.exe %random% > data.in
4     baoli.exe < data.in > baoli.out
5     youxiu.exe < data.in > youxiu.out
6     fc baoli.out youxiu.out
7     if not errorlevel 1 goto loop
8     pause
9     goto loop
View Code

非常容易懂,看不太懂没关系,会用就行。这里有一篇博客解释的很清楚,可以看一看:https://blog.csdn.net/wlx65003/article/details/51149196

至此,两种对拍已经结束。个人感觉还是更喜欢第二种hh

 

猜你喜欢

转载自www.cnblogs.com/jasmine-/p/11545493.html