版本比较
随机的艺术
正文
增加 14 行删除 14 行
12 行未改动13141516171718192021222324252627282728293031325 行未改动3839404142424344454625 行未改动727374757676777879798081828335 行未改动119120121122123124125123124125126127128129130129130131132133134134135136137138200 行未改动全部展开3393403413423433433443453463479 行未改动3573583593603613613623633643656 行未改动12 行未改动```以上的代码会得到一个 $[0,RANDMAX]$ 之间的数。```cpp//rand 生成随机数,以下函数对于 int 范围的 n 都能正常运行//随机数据生成 //随机数据生成#include <cstdlib>#include <ctime>int random(int n){ return 1ll*rand()*rand()%n;}int main(){ srand((unsigned)time(0)); //··· } //···}
```缺点:随机周期短,慢
5 行未改动#include <ctime>using namespace std;//...mt19937 rnd(time(0));mt19937_64 long_rnd(time(0)); mt19937_64 long_rnd(time(0));```前者是 `unsigned int` 范围,后者是 `unsigned long long` 范围。
两者都是一个类,用来定义随机数函数。调用直接像rand一样即可。25 行未改动
//随机生成树for(int i=2;i<=n;i++){ //从 [2,n] 之间的每个点向前面的点随机连一条边 //从 [2,n] 之间的每个点向前面的点随机连一条边 int fa=rnd()%(i-1)+1; int val=rnd()%1000000000+1; printf("%d %d %d\n",fa,i,val); printf("%d %d %d\n",fa,i,val);}
```
35 行未改动{ double s,myt,stdt; for(int i=1;i<=10000;i++) { system("data.exe > test.in");//测试的数据输入 test.in s=clock();//获取当前时间 system("test.exe < test.in > test.out");//将 test.in 中的数据输入 test.exe , 并将运行结果输入 test.out system("data.exe > test.in");//测试的数据输入 test.in s=clock();//获取当前时间 system("test.exe < test.in > test.out");//将 test.in 中的数据输入 test.exe , 并将运行结果输入 test.out myt=clock(); system("std.exe < test.in > std.out"); stdt=clock(); //同上 if(system("fc /W test.out std.out"))//fc 比较两者是否相同,不相同返回真。/W 过滤多余空格和换行 //同上 if(system("fc /W test.out std.out"))//fc 比较两者是否相同,不相同返回真。/W 过滤多余空格和换行 { printf("Wrong Answer, 测试点 #%d, 用时 %.0lfms, std用时 %.0lfms",i,myt-s,stdt-myt); return 0; }//时间的单位在 Windows 下是毫秒,需要换算自己手算就好了 }//时间的单位在 Windows 下是毫秒,需要换算自己手算就好了 else printf("Accepted, 测试点 #%d, 用时 %.0lfms, std用时 %.0lfms\n",i,myt-s,stdt-myt); } return 0;}200 行未改动全部展开{ db res=a[0],xx=1; for(int i=1;i<=n;i++) { xx*=x; res+=xx*a[i]; res+=xx*a[i]; } return res;}
9 行未改动 ll=ans-t; rr=ans+t; if(rr <= r && calc(rr) > calc(ans)) ans=rr; if(ll >= l && calc(ll) > calc(ans)) ans=ll; } printf("%.5lf\n",ans); printf("%.5lf\n",ans);// fclose(stdin); fclose(stdout); return 0;}```6 行未改动