如何对拍

idy002老师在放假为我们讲了讲c++的对拍。

以求1~n的和(n<=1e9)为例:

需要一个暴力程序(brute.cpp):

#include <cstdio>

int main() {
	int n;
	long long sum = 0;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		sum += i;
	printf("%I64d\n", sum);
}

以及一个正确程序(A.cpp):

#include <cstdio>

int main() {
	int n;
	scanf("%d", &n);
	long long sum = (1 + n) * (long long)n / 2;
	printf("%I64d\n", sum);
}

甚至可以是错误程序:

#include <cstdio>
#include <cstdlib>
#include <ctime>

int main() {
	int n;
	scanf("%d", &n);
	srand(time(0));
	if (rand() % 10 == 1)
	 n++;
	long long sum = (1 + n) * (long long)n / 2;
	printf("%I64d\n", sum);
}

不可或缺的数据生成器(gen.cpp):

#include <cstdio>
#include <cstdlib>
#include <ctime>

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

以及一个对拍程序(dp.cpp):

程序来自这里

system就跟cmd差不多?
大概意思如此吧。

#include<cstdio>
#include<cstdlib>
#include<ctime>

int main()
{   
    long s,t;
    while(1){
        system("cls");
        do{
            system("gen > input"); //data是数据生成程序
            s=clock();
            system("brute < input > brute.out");  //a是要交的程序
            t=clock();
            system("A < input > A.out");  //b是正确的程序
            if(system("fc brute.out A.out > nul"))
                break;
            else printf("AC time: %ldms\n",t-s);
        }while(1);
        printf("WA time: %ldms\n",t-s);  //运行时间 
        system("fc brute.out A.out");
        system("pause>nul");
    }
    return 0;
}

一切就绪,点击dp.exe即可开始AC(WA WA 机)的旅程。

考试之前真的有时间写这个吗?

我写Vim的配置文件就要写好久诶。

如果有时间还是学学吧。

真·放假来袭。

再见~

posted @ 2018-08-24 17:21  LoLiK  阅读(144)  评论(0编辑  收藏  举报