浴谷国庆集训 对拍

今天晚上集训ltt讲了 Linux 的简单操作和对拍,这里做一个简单的对拍笔记。

先来假设一个情景。现在是 NOIP 考试当中,有一道极难的 dp 题目,我无法证明它是否正确,但我能写出一个会超时但保证正确的暴力程序。

假设这段程序是要测试的 dp 程序 program.cpp:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <cstring>
 6 using namespace std;
 7 int main()
 8 {
 9     freopen("rand.out","r",stdin);
10     freopen("program.out","w",stdout);
11     int a,b;
12     cin >> a >> b;
13     if (a == 1)
14         cout << a+b << endl;
15     else
16         cout << a << endl;
17     return 0;
18 }

 

(显然这时错误的)

还有保证正确的暴力程序 std.cpp:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <cstring>
 6 using namespace std;
 7 int main()
 8 {
 9     freopen("rand.out","r",stdin);
10     freopen("std.out","w",stdout);
11     int a,b;
12     cin >> a >> b;
13     cout << a+b << endl;
14     return 0;
15 }

(一定正确的“暴力解法”)

再写一个随机生成数据的 rand.cpp:

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 using namespace std;
 5 int main()
 6 {
 7     freopen("rand.out","w",stdout);
 8     srand((unsigned)time(NULL));  
 9     cout << rand() << endl;
10     cout << rand() << endl;
11     return 0;
12 }

随机生成两个数据,将其输出在 rand.out 中。

然后在 std.cpp 和 program.cpp 中分别调用 rand.out 中随机生成的两个数据。

新建一个 cmd 文件,输入以下代码:

1 @echo off
2 :flag
3 rand.exe
4 program.exe
5 std.exe
6 fc program.out std.out
7 if not errorlevel 1 goto flag
8 pause

这段代码中,先执行随机生成数据,然后调用两个程序,最后利用 fc 比对 program.out 和 std.out 中的输出数据。

效果如图所示(这个批处理是另一位老师实现的,但是效果类似),发现错误后,可以在 rand.out 中找到让两个程序出现不同的数据,然后进行调试。

以上是有关对拍的笔记。

转载于:https://www.cnblogs.com/OIerPrime/p/7674663.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值