关于对拍

相信大家都听说过对拍。

如果没听说过也假装听说过吧

那么对拍是用来干什么的呢?

所谓对拍,是将你的程序跑出的答案与正确程序进行比较。具体方法是先写一个程序make来生成数据,再使用一个一定正确,但是不限制时间、空间复杂度的程序(暴力一般是可以保证正确性的)生成正确答案,然后再和你的程序运行出来的答案进行比较。你的程序有各种BUG,这个一般都可以产生错误数据供调试需要。

当然,想要对拍,数据生成是必须的(不然你拿什么拍)

这个时候我们便用到了随机数。

随机数的主要函数就是srand(time(0))和rand()

调用它需要头文件ctime和algorithm

比如我们现在要造一个需要我们排序的数据,即:

第一行输出一个n(n<=1000)

第二行输出n个整数,即每个数为A[i](a[i]<=1000000)

代码如下:

#include <ctime>
#include <cstdio>
#include <algorithm>
#define MAXN 1000007
using namespace std;
bool vis[MAXN];//标记 
int main(){
    freopen("IXIS.in","w",stdout);//不需要输入,只需要将数据输出到指定in文件中,此in文件将是std.cpp与my.cpp的输入文件 
    srand(time(0));
    int n = rand() % 1000 + 1;//若rand出的数大于自己想要的范围,%一下 
    printf("%d\n", n); 
    for(int i = 1; i <= n; i++){
        int p = rand() % 1000000 + 1;//同义 
        while(vis[p]) p = rand() % 1000000 + 1;//这里保证了p之前没有出现过即保证数不重复 
        printf("%d ", p); 
    }
    return 0;
}

那么接下来就是你要对拍的两个程序

//手写快速排序
#include <map>
#include <queue>
#include <string>
#include <vector> 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define MAXN 1000007
const int N = 1 << 21;
using namespace std;
int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') ch = getchar();
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}
void print(int x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
int getMax(int a[], int n) {
    int maxn;
    maxn = a[0];
    for(int i = 1; i < n; i++)
        if(a[i] > maxn) maxn = a[i];
    return maxn;
}
void countSort(int a[], int n, int exp) {
    int output[n];
    int buckets[10] = {0};
    for(int i = 0; i < n; i++)
        buckets[(a[i] / exp) % 10 ]++;
    for(int i = 1; i < 10; i++)
        buckets[i] += buckets[i - 1];
    for(int i = n - 1; i >= 0; i--) {
        output[buckets[(a[i] / exp) % 10] - 1] = a[i];
        buckets[(a[i] / exp) % 10]--;
    }
    for(int i = 0; i < n; i++) a[i] = output[i];
}

void radixSort(int a[], int n) {
    int exp, maxn = getMax(a, n);
    for(exp = 1; maxn / exp > 0; exp *= 10)
        countSort(a, n, exp);
}
int n, a[MAXN];
int main() {
    freopen("IXIS.in", "r", stdin);
    freopen("IXIS.my", "w", stdout); 
    n = read();
    for(int i = 0; i < n; i++) a[i] = read();
    radixSort(a, n);
    for(int i = 0; i < n; i++)
        print(a[i]), printf(" ");
    return 0;
}
//STL
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 1007
using namespace std;
int a[MAXN], n;
int main(){
    freopen("IXIS.in", "r", stdin);
    freopen("IXIS.std", "w", stdout);
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    for(int i = 1; i <= n; i++) printf("%d ", a[i]);
    return 0;
}

而对拍,这里只讲bat(才不是因为我不会qwq

代码为:

:loop
  maker.exe//生成数据的cpp弹出来的小黑框
  std.exe//STL的cpp弹出来的小黑框
  my.exe//手写快排的cpp弹出来的小黑框
  fc IXIS.std IXIS.my//两个程序的输出文件
  if %errorlevel%==0 goto loop//如果输出相同就一直进行下去
pause

emmm,记得到时候用的时候删掉后面的注释,否则,,会出现错误

因为这不是cpp的,所以不能放在dev中使用,其正确使用过程如下

1.建一个TXT文本文档(其实别的也可以啦,只要不是文件夹就行啦qwq)

2.将这个文本文档的后缀改为.bat

3.将这个文档用记事本打开,将代码贴进去

4.关闭,保存

5.双击,运行

如若两个程序输出没有不同则一直运行,若不同,停止运行,而此时IXIS.in里的数据为令两个程序输出不同的数据

若相同:

切它会一直疯狂的对比。

若不同:

有时候,在某些网站是下不了数据的,那么你就可以复制一篇题解(交上去)同你的程序对拍。

然后就会拍出你跑不过去的数据

OK,class is over.

我最讨厌不讲理的人了,但如果是你,撒个娇就好了。

猜你喜欢

转载自www.cnblogs.com/GTBD/p/10064153.html