10.7更新:见最下面

 

离NOIP2018没剩多长时间了,我突然发现我连对拍还不会,于是赶紧到网上找资料,找了半天发现了一个特别妙的程序,用c++写的!

 

不过先讲讲随机数据生成吧。

很简单,就是写一个程序模拟输入数据,然后利用rand()编写随机数。

在头文件cstdlib中,有rand(), srand()函数,以及常量RAND_MAX.

RAND_MAX在windos系统中为 0x7fff = 2^15-1 = 32767;在Unix(可以理解为linux,只不过linux是unix一种,是”类unix“)下为 2^31-1 = 2147483647(都是32位),加上一个正数最高位就为1,就变成了负数,即 -RAND_MAX = RAND_MAX + 1,所以 - rand() / RAND_MAX = rand() / (RAND_MAX + 1)。

rand()返回一个在0~RAND_MAX之间的整数。

srand(seed)函数接受一个unsigned int类型的参数seed,以seed为“随机种子”。

rand()函数基于线性同余递推式(就是某种递推式吧~)生成随机数,“随机种子“相当于这个递推式的初始参数,若不执行srand(),则种子默认为1.

当种子确定后,生成的随机数列也是固定的,因此这种随机方法是”伪随机“,所以一般要在开始调用srand(),且以当前时间为随机种子。

怎么写呢?ctime里有一个tim函数,调用time(0)可以返回从1970年1月1日0时0分0秒(Unix纪元)到现在的秒数。然后写这么一句就行了:srand((unsigned)time(0));

 

接下来才是重点:对拍~~

对拍是啥我就不多说了,直接讲怎么写。

首先,得有这么几个程序:

1.随机数据生成程序random.cpp.

2.自己的美妙算法程序sol.cpp

3.暴力程序bf.cpp (bf:brute force 暴力)

然后我们要写一个脚本,循环执行以下过程:

1.运行random.exe.

2.运行sol.exe

3.运行bf.exe

4.比对sol.out和bf.out的内容是否一致。

虽然Windos和类unix分别有bat批处理脚本和bash脚本,然而因为我太懒不想学别的语言,就废了半天功夫找到了一个用c++写的(你这道勤快)。

还是在cstdlib中,有一个system函数,他接受一个字符串作为参数,然后把这个字符串作为系统命令执行,栗如:system("C:\\Users\\Administrator\\Desktop\\random.exe"),执行windos桌面上的random.exe。

比对的话,windows用fc,类Unix用diff,他们接受两个文件路径,比较二者是否一致,若一致返回0,否则返回非0值。

下面放出windos的对拍程序,程序都是在桌面上的

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cctype>
 8 #include<vector>
 9 #include<stack>
10 #include<queue>
11 #include<ctime>
12 using namespace std;
13 #define enter puts("") 
14 #define space putchar(' ')
15 #define Mem(a) memset(a, 0, sizeof(a))
16 typedef long long ll;
17 typedef double db;
18 const int INF = 0x3f3f3f3f;
19 const int eps = 1e-8;
20 //const int maxn = ;
21 inline ll read()
22 {
23     ll ans = 0;
24     char ch = getchar(), last = ' ';
25     while(!isdigit(ch)) {last = ch; ch = getchar();}
26     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
27     if(last == '-') ans = -ans;
28     return ans;
29 }
30 inline void write(ll x)
31 {
32     if(x < 0) x = -x, putchar('-');
33     if(x >= 10) write(x / 10);
34     putchar(x % 10 + '0');
35 }
36 
37 int main()
38 {
39     for(int t = 1; t <= 100; ++t)
40     {
41         system("C:\\Users\\Administrator\\Desktop\\random.exe");
42         db Beg = clock();            //记录sol.exe的运行时间 
43         system("C:\\Users\\Administrator\\Desktop\\sol.exe");
44         db End = clock();
45         system("C:\\Users\\Administrator\\Desktop\\bf.exe");
46         if(system("fc C:\\Users\\Administrator\\Desktop\\sol.out C:\\Users\\Administrator\\Desktop\\bf.out"))
47         {
48             puts("WA"); return 0;
49         }
50         else printf("AC, #%d, Time : %.0lfms\n", t, End - Beg);
51     }
52     return 0;
53 }
View Code

相关文章:

  • 2021-08-25
  • 2021-07-09
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-02
  • 2022-01-08
  • 2021-08-05
  • 2021-07-28
  • 2022-12-23
  • 2021-11-30
  • 2021-09-13
相关资源
相似解决方案