随机数据生成与对拍【c++版,良心讲解】
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的对拍程序,程序都是在桌面上的
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const int eps = 1e-;
//const int maxn = ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int main()
{
for(int t = ; t <= ; ++t)
{
system("C:\\Users\\Administrator\\Desktop\\random.exe");
db Beg = clock(); //记录sol.exe的运行时间
system("C:\\Users\\Administrator\\Desktop\\sol.exe");
db End = clock();
system("C:\\Users\\Administrator\\Desktop\\bf.exe");
if(system("fc C:\\Users\\Administrator\\Desktop\\sol.out C:\\Users\\Administrator\\Desktop\\bf.out"))
{
puts("WA"); return ;
}
else printf("AC, #%d, Time : %.0lfms\n", t, End - Beg);
}
return ;
}
类unix上的对拍程序,只需要更改上面的代码system中的路径格式,把fc改成diff,用时单位该为”秒“(windos是毫秒)。
于是我又写了一个linux下的,奇特的是,双斜杠单斜杠竟然都行,而且时间还是以毫秒为单位……
#include<cstdio>
#include<ctime>
#include<cstdlib>
using namespace std;
typedef double db; int main()
{
for(int i = ; i <= ; ++i)
{
system("//home//noilinux//Desktop//random");
db Beg = clock();
system("//home//noilinux//Desktop//sol");
db End = clock();
system("//home//noilinux//Desktop//bf");
if(system("diff //home//noilinux//Desktop//sol.out //home/noilinux//Desktop//bf.out"))
{
puts("WA"); return ;
}
else printf("AC #%d, Time : %.0lfms\n", i, End - Beg);
}
return ;
}
这里给一个a + b的对拍程序:
首先是random.cpp
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const int eps = 1e-;
//const int maxn = ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int main()
{
freopen("random.in", "w", stdout);
srand((unsigned)time());
write(rand() % rand()); space; write(rand() % rand());
return ;
}
然后是sol.cpp(随便拷的一份LCT的……巨啊)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int data,rev,sum;
node *son[],*pre;
bool judge();
bool isroot();
void pushdown();
void update();
void setson(node *child,int lr);
}lct[];
int top,a,b;
node *getnew(int x)
{
node *now=lct+ ++top;
now->data=x;
now->pre=now->son[]=now->son[]=lct;
now->sum=;
now->rev=;
return now;
}
bool node::judge(){return pre->son[]==this;}
bool node::isroot()
{
if(pre==lct)return true;
return !(pre->son[]==this||pre->son[]==this);
}
void node::pushdown()
{
if(this==lct||!rev)return;
swap(son[],son[]);
son[]->rev^=;
son[]->rev^=;
rev=;
}
void node::update(){sum=son[]->sum+son[]->sum+data;}
void node::setson(node *child,int lr)
{
this->pushdown();
child->pre=this;
son[lr]=child;
this->update();
}
void rotate(node *now)
{
node *father=now->pre,*grandfa=father->pre;
if(!father->isroot()) grandfa->pushdown();
father->pushdown();now->pushdown();
int lr=now->judge();
father->setson(now->son[lr^],lr);
if(father->isroot()) now->pre=grandfa;
else grandfa->setson(now,father->judge());
now->setson(father,lr^);
father->update();now->update();
if(grandfa!=lct) grandfa->update();
}
void splay(node *now)
{
if(now->isroot())return;
for(;!now->isroot();rotate(now))
if(!now->pre->isroot())
now->judge()==now->pre->judge()?rotate(now->pre):rotate(now);
}
node *access(node *now)
{
node *last=lct;
for(;now!=lct;last=now,now=now->pre)
{
splay(now);
now->setson(last,);
}
return last;
}
void changeroot(node *now)
{
access(now)->rev^=;
splay(now);
}
void connect(node *x,node *y)
{
changeroot(x);
x->pre=y;
access(x);
}
void cut(node *x,node *y)
{
changeroot(x);
access(y);
splay(x);
x->pushdown();
x->son[]=y->pre=lct;
x->update();
}
int query(node *x,node *y)
{
changeroot(x);
node *now=access(y);
return now->sum;
}
int main()
{
freopen("random.in", "r", stdin);
freopen("sol.out", "w", stdout);
scanf("%d%d",&a,&b);
node *A=getnew(a);
node *B=getnew(b);
//连边 Link
connect(A,B);
//断边 Cut
cut(A,B);
//再连边orz Link again
connect(A,B);
printf("%d\n",query(A,B));
return ;
}
最后是bf.cpp
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const int eps = 1e-;
//const int maxn = ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int main()
{
freopen("random.in", "r", stdin);
freopen("bf.out", "w", stdout);
int a = read(), b = read();
write(a + b); enter;
}
把三个程序分别运行一下,得到三个exe。然后运行上面的对拍程序就行啦~~
附一下效果

如果把bf改成write(a +b + 1)的话:

这时候出错的数据就是当前的random.in了~~
***10月7日更新***
小云彩(tql)告诉我了一个更简便的写法。
在调用system的时候,里面可以不用写的那么复杂。如果这些东西在同一目录下,我们只用这么写:
#include<cstdio>
#include<cstdlib>
#include<ctime>
typedef double db;
using namespace std; int main()
{
for(int t = ; t <= ; ++t)
{
system(".\\random.exe");
db Beg = clock();
system(".\\ac.exe");
db End = clock();
system(".\\bf.exe");
if(system("fc .\\ac.out .\\bf.out"))
{
printf("WA\n"); return ;
}
else printf("#%d AC %.2lfms\n", t, End - Beg);
}
return ;
}
windos下用 .//,linux下是 ~\\。
方便。
对拍讲完啦~~~祝各位继续AKIOI~~
随机数据生成与对拍【c++版,良心讲解】的更多相关文章
- 一个比较全面的java随机数据生成工具包
最近,由于一个项目的原因需要使用一些随机数据做测试,于是写了一个随机数据生成工具,ExtraRanom.可以看成是Java官方Random类的扩展,主要用于主要用于测试程序.生成密码.设计抽奖程序等情 ...
- d3 使用随机数据生成条形图
).map(function(){ ,)(),); }) // 返回 [27.2, 12.9, 12.2, 6.8, 9.4, 7.1, 17.5, 30, 16.6, 24.3, 19, 16.6, ...
- 随机数据生成工具Mockaroo
测试用例生成工具:https://www.mockaroo.com/ 网站描述是:Random Data Generator and API Mocking Tool | JSON / CSV / S ...
- Perl 随机数据生成
问题:在IC设计及验证过程中,经常会遇到mem初始化的问题,这时候需要产生hex 的文件,本程序实现这种需求,只需要输入行数,及hex文件的宽度即可. print"Hello World!\ ...
- .NET使用Bogus生成大量随机数据
.NET如何生成大量随机数据 在演示Demo.数据库脱敏.性能测试中,有时需要生成大量随机数据.Bogus就是.NET中优秀的高性能.合理.支持多语言的随机数据生成库. Bogus的Github链接: ...
- .NET使用Bogus生成大量随机数据(转载)
原文地址:https://www.cnblogs.com/sdflysha/p/20190821-generate-lorem-data.html 在演示Demo.数据库脱敏.性能测试中,有时需要生成 ...
- irms模拟数据生成及数据分析
一.数据准备 1.每天生成随机一个文本,每小时向文本中追加2次数据,每次10万条 随机数据生成: 2,32 * * * * bash /mnt/jediael/irms/signalGenerat ...
- irms模拟数据生成及数据分析 分类: H_HISTORY 2015-03-06 14:17 212人阅读 评论(0) 收藏
一.数据准备 1.每天生成随机一个文本,每小时向文本中追加2次数据,每次10万条 随机数据生成: 2,32 * * * * bash /mnt/jediael/irms/signalGenerat ...
- 使用vs2010生成SQL Server 随机数据
前几天做测试数据,偶然发现vs2010中有一个生成随机数据的功能,记录下来,方便以后使用,确实非常的好用灵活快捷. 为了简单扼要的说明,下面我用一个实例来说明如何快捷使用: 在VS2010创建数据库项 ...
随机推荐
- 个人整理的 Windows 下 .NET 开发必装的软件
注: 最后更新时间:2019-03-15 一..NET 开发 1. 必装 软件名称 说明 下载地址 JetBrains Toolbox JetBrins 全家桶管理工具. 下载地址 JetBrains ...
- Storm入门(八)Storm实战常见问题总结(持续更新)
一.本地环境log级别设置问题 storm-core-1.1.0.jar下面有个log4j2.xml文件,默认log级别是info. <configuration monitorInterval ...
- ReactiveSwift源码解析(四) Signal中的静态属性静态方法以及面向协议扩展
上篇博客我们聊了Signal的几种状态.Signal与Observer的关联方式以及Signal是如何向关联的Observer发送事件的.本篇博客继续上篇博客的内容,来聊一下Signal类中静态的ne ...
- 关于单元测试的思考--Asp.Net Core单元测试最佳实践
在我们码字过程中,单元测试是必不可少的.但在从业过程中,很多开发者却对单元测试望而却步.有些时候并不是不想写,而是常常会碰到下面这些问题,让开发者放下了码字的脚步: 这个类初始数据太麻烦,你看:new ...
- java常用工具(jps等)说明
Java为我们提供了大量的工具辅助我们进行开发,位于jdk目录下的bin目录里,本篇博客将会随时更新相关工具的使用说明. jps 获取当前运行的java应用 lgj@lgj-Lenovo-G470:~ ...
- Java核心基础学习(一)--- 2019年1月
1.对比Exception和Error,运行时异常与一般异常 Exception 和 Error 都继承了 Throwable 类,在 Java 中只有 Throwable 类才能 thorw(抛出) ...
- 微信小程序初体验,入门练手项目--通讯录,后台是阿里云服务器(一)
内容: 一.前言 二.相关概念 三.开始工作 四.启动项目起来 五.项目结构 六.设计理念 七.路由 八.部署线上后端服务 同步交流学习社区: https://www.mwcxs.top/page/4 ...
- Java代码登录拦截器例子
通常我们在点击某个按钮的时候,对某个对象进行操作,是需要登陆才能做的,这时候就需要一个拦截器对某个方法进行拦截, 比如你在一个图书管理中心中你要借书,这时候你就会被要求出示借书证,管理员才能借书给你. ...
- Python编程从入门到实践笔记——操作列表
Python编程从入门到实践笔记——操作列表 #coding=utf-8 magicians = ['alice','david','carolina'] #遍历整个列表 for magician i ...
- keil教程之新建软件工程
前言 工欲善其事,必先利其器.要学好52单片机,就要会用keil写程序.不然,谈何学习单片机.下面介绍keil的使用. keil简介 Keil C51是美国Keil Software公司出品的51系列 ...