跳到正文

个人记录

关于对拍

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

文章信息

发布
2024年10月3日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 4 分钟

版本

  1. v1当前

我们知道:暴力写起来十分简单,虽然失掉了优秀的时间,但是可以保证准确性

我们又知道:一般情况的,在比赛或日常做题时的代码差不多确保时间达标,然而光靠我们造的那可怜几组的数据和考试给的,效果不好效率不行。

所以就有了

对拍!

大体的思路

对拍的构造过程说难也不难,说简单也没有那么简单:就是利用你的随机生成代码生成样例,然后运行你的对比代码,将你的暴力代码与比赛代码进行比较即可。

注意

  1. 保证你已经在随机生成样例处加上了随机数种子,不然所谓随机数只会生成单一数据!!
  2. 你的暴力代码,一定,一点要写对!!!!

比较完整的过程

代码
system("A.exe > A.txt") 

指的是运行 A.exe,把结果输出(>)到 A.txt 中。

代码
system("B.exe < A.txt > C.txt")

指的是运行 B.exe,从 A.txt 中读入(<)数据,把结果输出(>)到 C.txt 中。

代码
system("fc A.txt B.txt") 

指的是比较 A.txt 和 B.txt ,如果两个文件里的数据相同返回0,不同返回1。

对于样例的输出程式

C++
int num = rand() % 100 + 1;

为随机生成1~100的数

C++
 if(system("fc 1.out 1.ans > 1.log")) {
       printf("WA\n");
       verdict = 0;
       continue;
 }

也就是如果出现不相同的情况,就是wa

C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
    while (1) //一直循环,直到找到不一样的数据
    {
        system("data.exe > in.txt");
        system("baoli.exe < in.txt > baoli.txt");
        system("std.exe < in.txt > std.txt");
        if (system("fc std.txt baoli.txt")) //当 fc 返回 1 时,说明这时数据不一样
            break;                          //不一样就跳出循环
    }
    return 0;
}

完整的代码

C++
#include <iostream>
#include <cstdio>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    int ok = 0;
    int n = 50;
    for (int i = 1; i <= n; ++i)
    {
        system("data.exe > in.txt");
        system("std.exe < in.txt > std.txt");
        double begin = clock();
        system("baoli.exe < in.txt > baoli.txt");
        double end = clock();

        double t = (end - begin);
        if (system("fc std.txt baoli.txt"))
        {
            printf("测试点#%d Wrong Answer\n", i);
        }
        else if (t > 1000) //1秒
        {
            printf("测试点#%d Time Limited Exceeded 用时 %.0lfms\n", i, t);
        }
        else
        {
            printf("测试点#%d Accepted 用时%.0lfms\n", i, t);
            ok++; //AC数量+1
        }
    }
    printf("\n");
    double res = 100.0 * ok / n;
    printf("共 %d 组测试数据,AC数据 %d 组。 得分%.1lf。", n, ok, res);
}

是更加完善的

原链接-博客园

原文