ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

最强的对拍

2022-06-18 12:34:46  阅读:117  来源: 互联网

标签:rand return puts duipai arg 最强 include


点击查看代码
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
#include <sstream>
#include <stack>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <list>
#include <string>
#include <cstring>
#include <chrono>
using namespace std;
using namespace chrono;

string arg[10];

bool sys(string cmd)
{
    if (system(cmd.c_str()))
    {
        return 0;
    }
    return 1;
}

bool Compile(string filename)
{
    puts("--------------");
    if (!sys("g++ " + filename + ".cpp -o " + filename + " -O2 -Wall -Wno-unused-result -DDEBUG -fsanitize=address,undefined"))
    {
        return 0;
    }
    puts("Done");
    puts("--------------");
    return 1;
}

int main(int argv, char **argc)
{
    for (int i = 1; i < argv; ++i)
    {
        arg[i] = argc[i];
    }
    puts("--------------");
    printf("cpp: %s\n", arg[1].c_str());
    printf("std: %s\n", arg[2].c_str());
    printf("rand: %s\n", arg[3].c_str());
    if (arg[4] == "no")
    {
        puts("Disable compile");
    }
    else
    {
        puts("Enable compile");
    }
    if (arg[5] != "")
    {
        printf("Testcases: %d\n", stoi(arg[5]));
    }
    puts("--------------");
    if (arg[4] != "no")
    {
        puts("Compile cpp");
        if (!Compile(arg[1]))
        {
            return 0;
        }
        puts("Compile std");
        if (!Compile(arg[2]))
        {
            return 0;
        }
        puts("Compile rand");
        if (!Compile(arg[3]))
        {
            return 0;
        }
    }
    int n = -1;
    if (arg[5] != "")
    {
        n = stoi(arg[5]);
    }
    puts("Now begin test");
    puts("--------------");
    steady_clock::time_point t1, t2;
    duration<double> ut;
    for (int i = 1; i != (n + 1); ++i)
    {
        printf("Testcase %d\n", i);
        if (!sys("./" + arg[3] + " > duipai_rand.in"))
        {
            puts("Rand runtime error");
            return 1;
        }
        t1 = steady_clock::now();
        if (!sys("./" + arg[1] + " < duipai_rand.in > duipai_cpp_out.out"))
        {
            puts("Cpp runtime error");
            return 1;
        }
        t2 = steady_clock::now();
        ut = duration_cast<duration<double>> (t2 - t1);
        printf("Cpp used time: %.3lf ms\n", ut.count() * 1000);
        t1 = steady_clock::now();
        if (!sys("./" + arg[2] + " < duipai_rand.in > duipai_std_out.out"))
        {
            puts("Std runtime error");
            return 1;
        }
        t2 = steady_clock::now();
        ut = duration_cast<duration<double>> (t2 - t1);
        printf("Std used time: %.3lf ms\n", ut.count() * 1000);
        if (!sys("diff duipai_cpp_out.out duipai_std_out.out > diff.log"))
        {
            puts("Wrong Answer");
            return 1;
        }
        puts("--------------");
    }
}

环境:linux

用法:先编译,假设编译出来的文件名为 duipai

在终端内输入 ./duipai [你的代码文件名(去掉.cpp)] [标程代码文件名(去掉.cpp)] [随机数据生成器代码文件名(去掉.cpp)] [是否需要重新编译这三个文件,不需要填 no,需要任填或不填] [对拍次数]

实例:

需对拍的代码:(a.cpp)

点击查看代码
#include <cstdio>
#include <cstdlib>
#include <ctime>

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);

    // 如需测试程序运行时间功能,请取消注释这里
    // #include <unistd.h>
    // usleep(10000); // 让程序暂停多少微秒

    // 如需测试判断 WA 功能,请取消注释这里,该程序则会有 30% 的概率 WA
    // srand(time(0));
    // if (rand() % 10 <= 2)
    // {
    //     puts("sb");
    // }

    // 如需测试判断 RE 功能,请取消注释这里,该程序则会有 30% 概率 RE
    // srand(time(0));
    // if (rand() % 10 <= 2)
    // {
    //     printf("%d", 1 / 0);
    // }

    printf("%d\n", a + b);
}

标程:(std.cpp)

点击查看代码
#include <cstdio>
#include <cstdlib>

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

数据生成器 :(rand.cpp)

点击查看代码
#include <cstdio>
#include <cstdlib>
#include <ctime>

int main()
{
    srand(time(0));
    printf("%d %d\n", rand() % 100000, rand() % 100000);
}

使用指令:

./duipai a std rand

正常编译这三个代码,然后跑对拍

./duipai a std rand no

不编译,用上次编译出的程序对拍

./duipai a std rand 随便填 100

编译,对拍100次

./duipai a std rand no 100

不编译,对拍100次

标签:rand,return,puts,duipai,arg,最强,include
来源: https://www.cnblogs.com/eafoo/p/16388076.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有