跳到正文

科技·工程

对拍

Fluffy_fluffy约 5 分钟
原文
文章信息与版本

文章信息

发布
2024年1月8日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 5 分钟

版本

  1. v1当前

今天我们来学习对拍\text{今天我们来学习\Huge\textcolor{green}{{对拍}}}

目录

1 . 对拍是什么

2 . 对拍的组成

  • 暴力程序

  • 正解程序

  • 生成程序

  • 对拍程序

3 . 完美的 ACAC

1 . 对拍是什么

对拍是一种在 暴力做对,正解不会 的情况下调试正解的方法

2 . 对拍的组成

对拍由 44 部分组成

暴力程序

在对拍中,暴力程序是最核心的,因为只有暴力 ACAC 了,才可以对拍 以下以 P1001 A+B ProblemP1001~A + B~Problem 为例

以下为 P1001 A+B ProblemP1001~A + B~Problem 的暴力程序

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long

signed main()

{

 freopen("in.txt", "r", stdin);

 freopen("暴力.txt", "w", stdout);

 int a, b;

 cin >> a >> b;

 int c = 0;

 for (int i = 1; i <= a; i++) c++;

 for (int i = 1; i <= b; i++) c++;

 cout << c;

 return 0;
}

正解程序

以下为 P1001 A+B ProblemP1001~A + B~Problem 的错误正解程序

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long

signed main()

{

 freopen("in.txt", "r", stdin);

 freopen("正解.txt", "w", stdout);

 int a, b;

 cin >> a >> b;

 cout << a + b + 1;

 return 0;

}

生成程序

生成程序是用来随机生成样例的程序

以下为生成程序

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long

int some(int mod)

{

 //随机生成1到mod间的数

 int s = INT_MAX;

 return s * rand() % mod + 1;

}

signed main()

{

 freopen("in.txt", "w", stdout);

 srand(time(0));

 //使随机更随机

 int a = some(1e6);

 int b = some(1e6);

 cout << a << " " << b;

 return 0;

}

对拍程序

对拍程序通过先运行生成程序,在运行暴力程序和正解程序,并比较运行结果

以下为对拍程序

C++
#include <bits/stdc++.h>

using namespace std;

signed main()

{

 while (1)

 {

 	system("生成.exe");

 	system("暴力.exe");

 	system("正解.exe");

 	//运行代码

 	if (system("fc 暴力.txt 正解.txt"))

 	{

 		//比较

 		break;

 	}

 }

 return 0;

}

运行结果为


3 . 完美的 ACAC

不难看出正解比暴力大了 11 ,所以可以得出正确正解为

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long

signed main()

{

  freopen("in.txt", "r", stdin);

  freopen("正解.txt", "w", stdout);

  int a, b;

  cin >> a >> b;

  cout << a + b ;

  return 0;

}

运行结果…

原文