对拍程序的写法

对拍是什么呢?

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

怎么对拍呢?


首先,新建一个文件夹,

然后,在里面放入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

  • 37
    点赞
  • 111
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值