How to 对拍?

· · 个人记录

主要分成三个文件:

generator.cpp: 用来生成随机数

brute.cpp: 暴力代码(保证正确)

code.cpp: 待检验代码

duipai.cpp: 用来对拍

如果用A+B problem举例:

generator.cpp:

#include<bits/stdc++.h>
#include<ctime>
using namespace std;
int main() {
    srand(time(0));
    int a, b;

    for(int i=1;i<=20;i++)
    {
        a=(1ll * rand() * RAND_MAX+rand() )%200000;
        b=(1ll * rand() * RAND_MAX+rand() )%200000;
        printf("%d %d\n", a,b);
    } 
    return 0;
}

brute.cpp

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

int main() {
    int a, b;
    cin>>a>>b;
    cout<<a+b<<endl;
    return 0;
}

code.cpp

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

int main() {
    srand(time(0));
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", a+b);
    return 0;
}

duipai.cpp

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#define WHITE "\033[37m"
#define RED   "\033[31m"
#define YELLOW   "\033[33m"
#define GREEN   "\033[32m"
#define RESET "\033[0m"
using namespace std;

int main() {
    int expected = 0, verdict = 0,ok=0,maxt,nok=0,ans;
    printf("Input number of test cases: ");
    scanf("%d", &expected);
    printf("Input number of test max time: ");
    cin>>maxt;
    for(int test_case=1;test_case<=expected;test_case++) {
        cout<<RESET;
        printf("Running on test #%d: ", test_case);
        system("generator > 1.in");
        system("code < 1.in > 1.out");
        double begin = clock();
        system("brute < 1.in > 1.ans");
        double end = clock();

        double t = (end - begin);
        if(verdict==1)
        {
            cout<<RESET<<WHITE<<"Skip this test point"<<endl;
            continue;
        }
        else if(t>maxt)
        {
            cout<<YELLOW<<"Time Limited Exceeded on test"<<test_case<<" use "<<t<<" ms\n";
            continue;
        }
        else if(system("fc 1.out 1.ans > 1.log" ) and t<=maxt) {
            cout<<RED<<"Wrong Answer on test "<<test_case<<" use "<<t<<" ms\n";
            if(MessageBox(NULL,"Wrong Answer! Did you want to break?","break?",MB_YESNO|MB_ICONQUESTION)==IDYES)
            {
                verdict = 1;
                ans=test_case;
                continue;
            }
            verdict = 0;
            nok++;
            continue;
        }

        cout<<GREEN<<"Accepted on test "<<test_case<<" use "<<t<<" ms\n";
        ok++;
    }
    cout<<RESET;
    if(verdict)
    {
        cout<<YELLOW<<"You skip test on running test "<<ans<<endl;
        cout<<"You can debug!";
        return 0;
    }
    cout<<GREEN<<"AC:"<<ok<<"/"<<expected<<RESET<<RED<<"   WA:"<<nok<<"/"<<expected<<YELLOW<<"   TLE:"<<expected-ok-nok<<"/"<<expected<<endl<<RESET;

    if(float(ok*100.0/expected)<60)
    {
        cout<<RED;
    }
    else if(float(ok*100.0/expected)<=80)
    {
        cout<<YELLOW;
    }
    else  cout<<GREEN;
    cout<<"score:"<<float(ok*100.0/expected);
    return 0;
}