算法竞赛测试帮手--->对拍

对拍

由三个源文件构成

  • 一个是生成数据的源程序
  • 一个是由暴力做法一定正确的源程序
  • 一个是在暴力的基础上优化后的源程序

但是会用到批处理.bat文件

对拍:通过暴力程序去验证优化后的程序是否正确,称之为对拍(优化后的程序不知道正确性,但是可以通过暴力程序去验证优化后的程序是否正确)

生成数据源程序

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	srand((unsigned)time(0));
	int n;
	n = rand() % 20000 + 1; //2000是题中给出的数据范围,
	//题中给出的数据范围太大的话,可以适当缩小范围
	cout << n << endl; //随机给出一个数表示 n 
	for(int i= 1; i <= n; i++) //循环n次,每次n个随机数 
	{
		cout << rand() % 10000 + 1 << " ";
	} 
	return 0;
} 

批处理文件

后缀名是.bat, 用记事本打开,写下如下内容

:loop
gendata.exe > data.txt
baoli.exe < data.txt > baoli.txt
zhengjie.exe < data.txt > zhengjie.txt
fc baoli.txt zhengjie.txt
if %errorlevel%==0 goto loop
pause
解释:loop表示循环
1 > 2, 表示1的内容输入到2中
1 < 2  表示2的内容输入到1中
fc 1.txt 2.txt   表示1和2经行比较
if %errorlevel%==0 goto loop   //表示如果他们比较相等的话就会执行goto 到loop在执行不同数据的循环
pause 如果表示不一样的话,就会暂停,在终端中显示不同的各自的结果

双击.bat文件就可以运行程序进行自动比对
如果一直运行那么说明优化后的程序没有问题
如果程序停下来的,说明优化后的程序存在问题,在找bug就好了

这里以冒泡和快排为例

//冒泡:暴力
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
int n = 0, ans = 0;
int a[10010] = { 0 };

void sort1(int a[])
{
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < n - 1 - i; j++)
		{
			if (a[j] > a[j + 1]) { swap(a[j], a[j + 1]); }
		}
	}
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	sort1(a);
	for (int i = 0; i < n; i++)
	{
		if (i == 0) cout << a[i];
		else cout << " " << a[i];
	}
	return 0;
}
执行后出现.exe文件
//快排,优化
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
int n = 0, ans = 0;
int a[100010] = { 0 };

void sort1(int a[],int left, int right)
{
	if (left >= right) return;
	int i = left - 1, j = right + 1, mid = a[(left + right) / 2];
	while (i < j)
	{
		do i++; while (a[i] < mid);
		do j--; while (a[j] > mid);
		if (i < j)
		{
			swap(a[i], a[j]);
		}
	}
	sort1(a, left, i - 1);
	sort1(a, j + 1, right);
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	sort1(a,0,n -1);
	for (int i = 0; i < n; i++)
	{
		if (i == 0) cout << a[i];
		else cout << " " << a[i];
	}
	return 0;
}

//综上,准备着4个文件

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值