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

对拍

由三个源文件构成

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

但是会用到批处理.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个文件

本文链接:https://my.lmcjl.com/post/10214.html

展开阅读全文

4 评论

留下您的评论.