关于c++对拍的办法

众所周知,在(oi)学习以及考试中,对拍是一件非常重要的事。

有了对拍后,我们可以利用它发现代码上的一些非常难看出来的错误。

编写对拍程序一般有两个办法。

方案一

编写一个对拍的(cpp),并且利用文件操作和(fc),进行输出答案的判断。(fc)是系统的函数,可以判断两个文件是否一致。

注意:一定要将所有的(cpp)(exe)放置到一个文件夹

对拍代码如下:

#include<bits/stdc++.h>
using namespace std;
int Case=0;
int main() {
	while(1) {
		system("Make_date.exe");
		system("Ac.exe");
		double st=clock();
		system("Wa.exe");
		double ed=clock();
		printf("第%d个测试点:",++Case);
		if(system("fc Ac.out Wa.out"))return !puts("Wrong Answer");
		printf("Accept,耗时:%.3fms
",ed-st);
	}
}

数据生成器如下:

#include<bits/stdc++.h>
using namespace std;
int Random(int n){
	return rand()*rand()%n;
}
int main() {
	srand(time(0));
	freopen("data.in","w",stdout);
	//主程序生成。。。
}

WA代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

#define reg register
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define debug(x) cerr<<#x<<" = "<<x<<" ";
#define LL long long

inline int Read() {
	int res=0,f=1;
	char c;
	while(c=getchar(),c<48||c>57)if(c=='-')f=0;
	do res=(res<<3)+(res<<1)+(c^48);
	while(c=getchar(),c>=48&&c<=57);
	return f?res:-res;
}

template<class T>inline bool Min(T &a,T const&b) {return a>b?a=b,1:0;}
template<class T>inline bool Max(T &a,T const&b) {return a<b?a=b,1:0;}
const int N=1e5+5,T=1<<22;

int main() {
	freopen("data.in","r",stdin);
	freopen("Wa.out","w",stdout);
	// 错误代码。。。
}

AC代码:

#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
int main() {
	freopen("data.in","r",stdin);
	freopen("Ac.out","w",stdout);
    // AC代码。。。
}

不过,这个方法最为麻烦的事情就是文件操作极其烦人。

每次不能直接复制粘贴,还要编译运行。

那么,另一种方法就出现了。。

方法二

利用windows环境下的批处理进行对拍。

怎么利用批处理文件对拍呢?

首先,我们要先新建一个后缀为(.bat)的文件。

然后将下列代码复制进去就行了,不用文件操作直接双击即可。

代码如下:

@echo off  
:loop  
    Make_data.exe>data.in  //随机样本
    Ac.exe<data.in>Ac.out //正确代码
    Wa.exe<data.in>Wa.out  //测试代码
    fc Ac.out Wa.out  
if not errorlevel 1 goto loop  
pause  
goto loop

(update.in——2019.11.13)
这里再附上一个同样原理的(c++)编写的对拍程序,利用我们熟知的(c++)代码,可以使我们的对拍更加的多样和细致。
代码如下:

#include<bits/stdc++.h>
using namespace std;
int main() {
	int tot=0;
	while(1) {
		system("Make_data.exe >date.in");
		system("AC.exe <date.in >AC.out");
		double st=clock();
		system("WA.exe <date.in >WA.out");
		double ed=clock();
		if(!system("fc AC.out WA.out"))printf("第%d个测试点:Accepted,用时:%.2lfms
",++tot,ed-st);
		else {
			printf("第%d个测试点:Wrong Answer 
",++tot);
			break;
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/dsjkafdsaf/p/11258714.html