对拍

以前没学过对拍,临近 csp 了才学,记录一下。

对拍:

就是你在 OI 赛制下不确定你的做法是否正确,就可以模拟评测机来检测,如果拍了很久还没出错,大概率不会出错了。

具体的,你有一份你写的程序,你还需要写:

  • 一份保证正确但可能超时的暴力。

  • 一个可以输出随机数据的数据

  • 一个检测程序来检测你写的程序和暴力输出的是否一样。

我们以 a + b 问题举例:

假设我们写了一份代码:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  cout << a + b;
  return 0;
}

我们现在不知道这份代码的正确性,于是我们要写一份暴力:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  for (int i = -100000; i <= 100000; i++) {
    if (a + b == i) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}

随机数据可以参考网上的。这里给出我的:

#include <bits/stdc++.h>

using namespace std;

int main() {
  srand(time(NULL)); // 随机种子
  int x = rand() % 1919810 + 1;
  int y = rand() % 1919810 + 1;
  cout << x << " " << y << endl;
  return 0;
}

那好,现在只要知道 dubug 程序怎么些就好了。

这里给出模板:

#include <bits/stdc++.h>

using namespace std;

int main() {
  while (1) {
    system("debug.exe > in.txt");  
    system("1.exe <in.txt > outstd.txt");
    system("2.exe < in.txt > outbl.txt");
    if (system("fc outstd.txt outbl.txt")) {
      break;
    }
  }
  return 0;
}

其中 debug 是随机数程序,system("debug.exe > in.txt") 的意思是运行 debug 程序造出随机数放入 in.txt 里面。

system("1.exe < in.txt > outstd.txt"); 中的 \(1\) 是需要评测的程序,这句话的意思是 \(1\) 号程序运行输入 in.txt 里面的东西,把输出的结果放在 outstd.txt 里面。system("2.exe < in.txt > outbl.txt"); 同理。

system("fc outstd.txt outbl.txt") 的意思是:比较 outstd.txtblout.txt 的内容,如果相同返回 \(0\),否则返回 \(1\)

但是每次你修改程序后都要运行形成 .exe 后缀的文件,建议你把这四个程序都放在同一个文件夹里。

posted @ 2023-10-07 14:25  ydq1101  阅读(123)  评论(0)    收藏  举报