对拍 & 随机数生成

用 Windows 批处理对拍:

1. 新建一个批处理(.bat),代码如下:

:loop
@echo off data_creator.exe force_solution.exe correct_solution.exe
@echo on fc force_solution.out correct_solution.out
if not errorlevel 1 goto loop pause

2. 创建一个数据生成程序,一个暴力程序,一个正解。

3. 循环运行这个批处理,对照 correct_solution 和 force_solution 的答案是否相同。


随机数生成器 Example(Data_Creator.cpp)

The function random(a, b) creates integers in the range [a, b]. The function randll(a, b) creates Int64 integers in the range [a, b].

Sentence srand(time(0)); is needed.

 1 /* Data Creator
 2  * Au: GG
 3  */
 4 #include <cstdio>
 5 #include <cstdlib>
 6 #include <cstring>
 7 #include <cmath>
 8 #include <ctime>
 9 #include <iostream>
10 #include <algorithm>
11 using namespace std;
12 
13 // Create a random in range [a, b].
14 // It can only create integers.
15 int random(int a, int b) {
16     if (rand() % 2)
17         return (rand() * 32768 + rand()) % (b - a + 1) + a;
18     else return (rand() * 32768 - rand()) % (b - a + 1) + a;
19 }
20 
21 // Create a random in range [a, b]
22 // Supports Int64 numbers.
23 long long randll(long long a, long long b) {
24     if (rand() % 2)
25         return ((long long)random(0, 2147483647) * 2147483648ll + random(0, 2147483647)) % (b - a + 1) + a;
26     else return ((long long)random(0, 2147483647) * 2147483648ll - random(0, 2147483647)) % (b - a + 1) + a;
27 }
28 
29 int n1, n2, a1, b1, a2, b2;
30 
31 int main() {
32     freopen("data.out", "w", stdout); // write to file
33     srand(time(0)); // create a random seed
34 
35     scanf("%d%d%d", &n1, &a1, &b1);
36     scanf("%d%d%d", &n2, &a2, &b2);
37     for (int i = 1; i <= n1; i++)
38         printf("%d
", random(a1, b1));
39     for (int i = 1; i <= n2; i++)
40         printf("%lld
", randll(a2, b2));
41 
42     return 0;
43 }

srand(time(NULL)) 也是可行的,但不 secure。参见 https://stackoverflow.com/questions/26206780/is-srandtimenull-bad

另外一种对拍方法:“管道”,即命令行参数。参见 http://blog.csdn.net/wlx65003/article/details/51149196


Post author 作者: Grey
Copyright Notice 版权说明: Except where otherwise noted, all content of this blog is licensed under a CC BY-NC-SA 4.0 International license. 除非另有说明,本博客上的所有文章均受 知识共享署名 - 非商业性使用 - 相同方式共享 4.0 国际许可协议 保护。
原文地址:https://www.cnblogs.com/greyqz/p/rand.html