跳到正文

科技·工程

对拍器

Ydch约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年10月25日
收录
2026年10月25日
更新
2026年11月21日
篇幅
约 4 分钟

版本

  1. v1当前

对拍,用来检验代码是否正确。

对拍的流程

  1. 生成一组随机数据。
  2. 暴力代码和 std 运行这组数据。
  3. 比较输出是否错误。

对拍代码

以下均为在同目录下运行结果。

我们需要:std.cpp bf.cpp rand.cpp check.cpp 以及他们的 .exe 文件,分别表示 std,暴力代码,数据生成器,对拍器;以及三个文件 std.in std.out std.ans,表示输入,std 输出和暴力输出。

std.cpp & bf.cpp

放入对应代码并运行生成 .exe 文件即可,文件输入指向 std.in,std 输出指向 std.out,bf 输出指向 std.ans。

rand.cpp

随机数据生成器,建议使用 mt19937,用 rand 和 timeb 也可以。按照题目输入格式生成随机数,文件输出指向 std.in。

check.cpp

对拍器,循环跑对拍,先运行 system("rand.exe") 生成随机数据,接着运行 system("std.exe") 和 system("bf.exe") 获得输出文件,再用 system("fc /w std.out std.ans") 对比文件输出。

  • fc 指令在相同时返回 00,fc /w 指(为 Microsoft Learn 原文):在比较过程中压缩空格(即制表符和空格)。 如果一行包含许多连续的空格或制表符,则 /w 将这些字符视为单个空格。 与 /w 一起使用时, fc 会忽略行首和行尾的空格。

示例

以 P9748 为例。

主要展示 rand.cpp check.cpp。

C++
// rand.cpp
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 5e3; // Max N
random_device Random;
mt19937 gen(Random());

inline int ri(int min, int max) { // 生成 min ~ max 范围内随机数
    uniform_int_distribution<> rdrd(min, max);
    return rdrd(gen);
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    freopen("std.in", "w", stdout);
    cout << ri(1, N) << '\n';
    return 0;
}
C++
// check.cpp
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 100; // 数据组数
int AC = 0, WA = 0;

signed main() {
    for (int T = 1; T <= N; T++) {
        system("rand.exe");
        system("std.exe");
        system("bf.exe");
        if (system("fc /W std.out std.ans >nul 2>nul")) { // 返回 1 表示不相同
            printf("Case #%lld : Wrong Answer\n", T), WA++;
            system("type std.in & echo ---- & type std.out & echo ---- & type std.ans"); // 输出三个文件
        }
        else
            printf("Case #%lld : Accepted\n", T), AC++;
    }
    printf("Accepted : %lld, Wrong Answer : %lld\n", AC, WA);
    return 0;
}
原文