在Linux or Windows中如何优雅的写出对拍

一、前言

网上的对拍程序层出不穷,大多LinuxWindows中的对拍程序都是独立开的(在Windows中用.bat,在Linux中用.sh),那么有没有一种方法使之统一起来呢?🤔答案是有的!🥳

二、结论

对于有基础的同学直接看结论就行了。用Cppsystem()函数调用系统命令,来写对拍程序就可以了。其中记住以下几点就行

  1. Windows中可执行文件后缀名为.exe,Linux中可执行文件后缀名为.out。在对应系统生成对应程序。(这个应该都没问题把🧐)

  2. 对拍的核心就是判断绝对AC的程序与现有程序输出是否一致

    • Linux中使用diff
    • Windows中使用fc

1、对拍

Windows

#include<bits/stdc++.h>
using namespace std;

void slove() {
    while (true) {
        system("gen.exe > tmp.in");
        system("F.exe < tmp.in > tmp.out");
        system("F_AC.exe < tmp.in > tmp_AC.out");
        if (system("fc tmp.out tmp_AC.out")) {
            cout << "WA" << endl;
            return;
        } else cout << "AC" << endl;
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    /*cin >> t;*/
    while(t--) slove();
    return 0;
}

Linux

#include<bits/stdc++.h>
using namespace std;

void slove() {
    while (true) {
        system("gen.out > tmp.in");
        system("F.out < tmp.in > tmp.out");
        system("F_AC.out < tmp.in > tmp_AC.out");
        if (system("diff tmp.out tmp_AC.out")) {
            cout << "WA" << endl;
            return;
        } else cout << "AC" << endl;
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    /*cin >> t;*/
    while(t--) slove();
    return 0;
}

三、对拍详解

1、什么是对拍呢?🧐

判断绝对AC的程序与现有程序输出是否一致

简单来说,用代码生成 输入数据,再把生成的输入数据分别喂给已知能AC的程序和不确定能AC的程序,找出让这两个程序输出不一致的 输入数据

2、对拍的组成部分

对拍的组成部分

3、输入数据生成

目标:用程序随机自动生成输入数据
做法:用srand() + time()函数生成随机数
注意的点

  • Windowssrand()随机数的范围为0 ~ 32767
  • Linuxsrand()随机数的范围为0 ~ 2147483647
  • 哪我们如果数据范围在 long long怎么办呢?请看我封装的random()函数
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
using namespace std;

/*
// random ---> 生成数据范围在[l,r]的随机数
// Linux
ll random(int l, int r) { 
	return (ull)rand() * rand() % (r - l + 1) + l;
}
*/

// Windows
ll random(int l, int r) { 
	return (ull)rand() * rand() * rand() * rand() % (r - l + 1) + l;
}

void slove() {
    srand((unsigned)time(0));
    int n = random(1, 10);
	cout << n << endl;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    /*cin >> t;*/
    while (t--) slove();
    return 0;
}

4、对拍程序

基础知识

  • F.exe < tmp.in 代表输入重定向。它的作用是将一个文件的内容作为输入传递另一个程序。
  • Linux中的diffWindows中的fc,代表比较两个文件中的内容是否一致。

Windows

#include<bits/stdc++.h>
using namespace std;

void slove() {
    while (true) {
        system("gen.exe > tmp.in");
        system("F.exe < tmp.in > tmp.out");
        system("F_AC.exe < tmp.in > tmp_AC.out");
        if (system("fc tmp.out tmp_AC.out")) {
            cout << "WA" << endl;
            return;
        } else cout << "AC" << endl;
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    /*cin >> t;*/
    while(t--) slove();
    return 0;
}

Linux

#include<bits/stdc++.h>
using namespace std;

void slove() {
    while (true) {
        system("gen.out > tmp.in");
        system("F.out < tmp.in > tmp.out");
        system("F_AC.out < tmp.in > tmp_AC.out");
        if (system("diff tmp.out tmp_AC.out")) {
            cout << "WA" << endl;
            return;
        } else cout << "AC" << endl;
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    /*cin >> t;*/
    while(t--) slove();
    return 0;
}

5、操作流程

执行对拍程序就可以等待,程序停止,程序停止就代表程序找到WA的数据。这时我们只需要打开tmp.in查看这份数据就可以啦!🥳

四、最后

创作不易,如有帮助,点个赞鼓励一下吧!万分感谢!!!😭

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值