002.简易对拍器

对拍

假设我们拿到这样一个题目:

求和

Input

两行,第一行一个整数n(2<=n<=100)

第二行n个整数A1,A2……An (-100<=Ai<=100)

Output

这n个整数的和

一、应用条件:

1 . 一份完全正确的代码:stand.cpp

可以是自己写的大暴力,也可以是别人已经ac的代码

比如我们可以写出这样一个暴力:

#include<iostream>
int main(){
    int n,a,ans=0;
    std::cin>>n;
    for(int i=0;i<n;i++){
        std::cin>>a;
        if(a>=0){
            for(int j=0;j<a;j++){
                ans++;
            }
        }
        else{
            for(int j=0;j<a;j++){
                ans--;
            }
        }
    }
    std::cout<<ans;
}

假如我还没有学过加法,只会掰着手指头记数,就会写出这样一段代码

显然,不考虑复杂度,这是正确的


2 . 模拟随机的输入:rand.cpp

  • Sample Input格式一致
  • 数据范围完备合法
#include<bits/stdc++.h>
int main(){
    std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
    //生成一个随机的64位数[0,2^64-1]
    long long L=2;
    long long R=100;
    std::uniform_int_distribution<long long> dist(L, R);
    long long a=dist(rng);
    //生成的a是一个[2,100]的随机数
    printf("%lld\n",a);
    L=-100;
    R=100;
    for(int i=0;i<a;i++){
        printf("%lld ",dist(rng));
    }
    //生成a个[-100,100]的随机数
}

通过上面的程序我们可以得到这样的Input

//input:
7
4 -82 45 26 -61 2 99

3 . 待测试代码: text.cpp

可能存在bug的代码

#include<iostream>
int main(){
    int n,a,ans=0;
    std::cin>>n;
    for(int i=0;i<n;i++){
        std::cin>>a;
        if(a==64)ans+=54;
        else ans+=a;
    }
    std::cout<<ans;
}

由于想不到会出现什么bug,我选择手动添加一个

这是一个明显的错误,但更多情况下bug会很隐蔽


二、对拍器实现

duipai.cpp实现

#include <bits/stdc++.h>
using namespace std;
//手动实现比较函数
bool compareFiles(const string& file1, const string& file2) {
    ifstream f1(file1), f2(file2);
    string s1, s2;
    ostringstream oss1, oss2;
    oss1 << f1.rdbuf();
    oss2 << f2.rdbuf();
    s1 = oss1.str();
    s2 = oss2.str();
    while (!s1.empty() && isspace(s1.back())) s1.pop_back();
    while (!s2.empty() && isspace(s2.back())) s2.pop_back();
    return s1 == s2;
}
int main() {
    int test_case = 1;
    while (true) {
        //生成随机数据
        system("rand.exe > input.txt");
        
        //stand.cpp和text.cpp分别处理数据
        system("stand.exe < input.txt > stand_out.txt");
        system("text.exe < input.txt > text_out.txt");
        
        //比较输出
        if (!compareFiles("stand_out.txt", "text_out.txt")) {
            cout << "\nWA\n";
            break;
        }
        cout << "AC #" << test_case << endl;
        test_case++;
    }
    
    return 0;
}

最后出错的信息会留在下面三个txt文件中

input.txt

stand_out.txt

text_out.txt


简易脚本封装

为了方便使用,实现如下两个bat文件:

run.bat

@echo off
cls
echo 编译中...
echo.

:: 编译所有程序
g++ -o rand.exe rand.cpp -std=c++17 -O2 2>nul
if errorlevel 1 (echo ? rand.cpp 失败 & pause & exit)
echo ? rand

g++ -o stand.exe stand.cpp -std=c++17 -O2 2>nul
if errorlevel 1 (echo ? stand.cpp 失败 & pause & exit)
echo ? stand

g++ -o text.exe text.cpp -std=c++17 -O2 2>nul
if errorlevel 1 (echo ? text.cpp 失败 & pause & exit)
echo ? text

g++ -o duipai.exe duipai.cpp -std=c++17 -O2 2>nul
if errorlevel 1 (echo ? duipai.cpp 失败 & pause & exit)
echo ? duipai

echo.
echo 编译成功
echo.
echo Ctrl+C 退出
echo.

duipai.exe
pause

clean.bat

@echo off >nul 2>&1
if exist input.txt (
    (echo Sample Input & type input.txt & echo. & echo Sample Output & type stand_out.txt & echo. & echo Wa Output & type text_out.txt) > WA.txt 2>nul
)
del input.txt stand_out.txt text_out.txt *.exe 2>nul >nul

三、使用方法

初始配置

1、新建文件夹

添加2个txt文件,4个cpp文件:

run.txt

clean.txt

duipai.cpp

rand.cpp

stand.cpp

text.cpp

2、配置duipai.cpp , run.bat , clean.bat

复制粘贴,然后改后缀即可

使用

1、实现rand.cpp

2、将stand.cpp,text.cpp粘贴进来

3、双击ran.bat

这会产生一些exe、txt文件

4、双击clean.bat

5、查看WA.txt


未来使用时只需

  • 修改rand.cpp

  • 粘贴stand.cpp、text.cpp

  • 运行run.bat、clean.bat


四、结语

注意:即使确保rand.cpp正确、stand.cpp正确,也不能100%找出bug

感谢观看

如果能对你有所帮助,不胜荣幸

辅助rand

为了 偷懒 节约时间,实现以下功能

#include <bits/stdc++.h>
using namespace std;
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T>void pri(T l, T r) {
    uniform_int_distribution<long long> dis(l, r);
    cout<<dis(rng);
}

template<typename T>void pri(T l, T r, char op) {
    uniform_int_distribution<long long> dis(l, r);
    cout<<dis(rng)<<op;
}

template<typename T>T get_rand(T l, T r) {
    uniform_int_distribution<T> dis(l, r);
    return dis(rng);
}

template<typename T>void pri_arr(int len, T l, T r) {
    uniform_int_distribution<long long> dis(l, r);
    for (int i = 1; i < len; i++) {
    cout<<dis(rng)<<' ';
    }
    cout<<dis(rng)<<'\n';
}

int main() {
    cout<<"1\n";
    int n=get_rand(1,10);
    cout<<n<<' ';
    pri(1,20,'\n');
    pri_arr(n,-20,20);
    pri_arr(n,-10,10);
}

更新于2025.12.13

posted @ 2025-12-07 18:07  射杀百头  阅读(25)  评论(0)    收藏  举报