123789456ye

已AFO

c++对拍程序写法

以下方法所有程序都必须在同一个文件夹(或者你想打一长串文件路径也行)

std是标程,test是暴力(反正要是对的),data是造数据的

Ubuntu下你需要换的东西:
运行程序从std.exe换成./std
比较命令改成"diff test.out std.out -b -B -w"


第一种

好处:简单易写,不用开文件io
坏处:不能显示运行时间
ps:/W 是忽略空格和空行(一般评测的做法)

@echo off
:again
data>in.txt
std<in.txt>stdout.txt
test<in.txt>testout.txt
fc /W stdout.txt testout.txt >nul
if not errorlevel 1 goto again
pause

建一个txt文件复制进去再改为bat文件,运行即可
ubuntu下:建一个check.sh,chmod 777 然后进终端运行

#!/bin/bash                         
g++ test.cpp -o test -Wall
g++ data.cpp -o data -Wall
g++ std.cpp -o std -Wall  
while true;do                           
    ./data>data.in
    ./test<data.in>test.out
    ./std<data.in>std.out      
    if diff -b -B -W test.out std.out;then    
        echo "Accepted"                             
    else
        echo "Wrong Answer!"
        break                                      
    fi                                         
done                         

第二种

好处:不用开文件io并且没有回显
坏处:码量比第一种略大
主要就是把第一种的cmd语句搬过来再加上>nul取消回显

#include<bits/stdc++.h>
using namespace std;
double clock(double x)
{
    return x*1000/CLOCKS_PER;
}
int main()
{
    clock_t s,t,t2;
    register int i=1;
    for(;;++i)
    {
        system("data.exe > in.txt");
        s=clock();
        system("std.exe < in.txt  > stdout.txt");
        t=clock();
        system("test.exe < in.txt > testout.txt");
        t2=clock();
        if(system("fc /W stdout.txt testout.txt > nul"))
        {
			printf("point #%d\nWA time used: std %.2lfms test %.2lfms\n",i,clock(t-s),clock(t2-t));
			break;
        }
		printf("point #%d\nAC time used: std %.2lfms test %.2lfms\n",i,clock(t-s),clock(t2-t));
    }
    system("pause>nul");
    return 0;
}

upd:根据ljq学长(\(J\)\(\color{red}{esseLiu}\))的说法可以用srand((unsigned long long)new char)来实现比一秒一次更快的更新频率。它返回的是内存地址(好高级啊)

posted @ 2019-06-21 20:42  123789456ye  阅读(746)  评论(0编辑  收藏  举报