对拍教程(自用)

写在前面:

没有掌握对拍啊,每次都是上网贺别人的代码,写一份自己用吧。

对拍,是一个比较实用的工具。它能够非常方便地对于两个程序的输出文件进行比较,可以帮助我们实现一些自动化的比较输出结果的问题。

Step1:准备

很明显,有对才有拍,你要一份暴力代码,一份你的正解。
编译后放在同一个文件夹中。

Step2:数据生成器

生成随机数:

#include<sys/timeb.h>
signed main(){
	struct _timeb T;
	_ftime(&T);
	srand(T.millitm);
}

在下方使用 x=rand()就会生成随机数。
Windows 系统下 rand() 生成的随机数的范围在 0~32767

Step 3: 操作

  • 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。

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

取经博客

注意事项:

A,B,数据生成器都不能写freopen

posted @ 2025-10-19 20:01  rerecloud  阅读(4)  评论(0)    收藏  举报