如何对拍

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的配置文件就要写好久诶。

如果有时间还是学学吧。

真·放假来袭。

再见~

转载于:https://www.cnblogs.com/LoLiK/p/9531022.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值