# 前言

刚开始学 OIOI 的时候学过对拍,但是后来基本上没有用过。

(暴力都不会写什么对拍)

临近 CSPCSP 稍微复习了一下,于是写一篇博客记录一下(以后忘了还可以看)。

# 正文

# 方法

# 1. 准备好你需要对拍的程序 a.cpp

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

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

# 2. 然后是你的暴力代码 b.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdio>

using namespace std;

int main(){
long long a, b;
cin >> a >> b;
long long ans = 0;
for(int i = 1; i <= a; ++i)
for(int j = 1; j <= b; ++j)
ans++;
cout << ans << endl;
return 0;
}

# 3. 生成随机数据Data.cpp

注意要输出出来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <windows.h>
#include <ctime>

using namespace std;

int main(){
srand(time(0));
int a = rand(), b = rand();
cout << a << " " << b << endl;
return 0;
}

# 4. 对拍程序test.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <windows.h>

using namespace std;

int main(){
while(true){
//生成数据存到 Data.txt 中
system("Data.exe > Data.txt");

//运行 a.cpp 及 b.cpp 并把结果分别存到 a.txt 和 b.txt 中
system("a.exe < Data.txt > a.txt");
system("b.exe < Data.txt > b.txt");

//判断结果是否相同 (a.txt 与 b.txt 是否完全一致)
if(system("fc a.txt b.txt"))
break;
}
cout << "Error!" << endl;
system("Pause");
return 0;
}

  1. 把这几个文件都放到同一个目录下,并生成相应的 .exe 文件。然后点击 test.exe 即可观察到对拍结果。

# 结果

  • 如果都一样,会有如下输出:

  • 如果结果不一样,那么会有如下提示:

这时你可以点开你的 Data.txt 文件,观察输入数据

# 注意事项

输入输出格式一定要一致!
四个程序要放到同一个文件夹里!

阅读次数