zoukankan      html  css  js  c++  java
  • 如何对拍

    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
    ", sum);
    }
    
    

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

    #include <cstdio>
    
    int main() {
    	int n;
    	scanf("%d", &n);
    	long long sum = (1 + n) * (long long)n / 2;
    	printf("%I64d
    ", 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
    ", sum);
    }
    
    

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

    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    
    int main() {
    	srand(time(0));
    	printf("%d
    ", 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
    ",t-s);
            }while(1);
            printf("WA time: %ldms
    ",t-s);  //运行时间 
            system("fc brute.out A.out");
            system("pause>nul");
        }
        return 0;
    }
    
    

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

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

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

    如果有时间还是学学吧。

    真·放假来袭。

    再见~

  • 相关阅读:
    复利完结观看评价
    汉堡计划
    0406 复利计算完结篇
    构建之法 第四章 读后感
    复利计算单元测试
    实验一 cmd命令的编写
    构建之法 前三章读后感
    复利计算完成总结
    复利计算软件v3
    实验 0 了解和熟悉操作系统
  • 原文地址:https://www.cnblogs.com/LoLiK/p/9531022.html
Copyright © 2011-2022 走看看