随机数据生成与对拍【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-删除Windows Server backup卷影副本
现有环境中有一台Windows Server做过定期备份计划,时间太久未做清理操作,收到磁盘报警邮件后需要及时释放该空间,具体操作步骤如下: 当前备份计划信息如下: 清理步骤如下: 1.以管理身份运行 ...
- 判断HTML中的checkbox是否被选中
//合法性验证 function checkValidity() { var userNameCheck = $("#userNameCheck").attr('checked') ...
- 使用 connect http proxy 绕过 ssh 防火墙限制
1.安装 connect brew install connect 2.配置 ~/.ssh/config Host * ProxyCommand connect -H your.proxy.serve ...
- 从一道面试题探究 Integer 的实现
记得有次面试,面试官问我: 如何写一个方法交换两个 Integer 类型的值? 当时心里一惊,这是把我当小白了呀!交换两个数的值还不容易么,最简单的直接搞一个中间变量,然后就可以交换了… … 面试官随 ...
- kafka的设计
1.动机 设计 kafka 初衷,作为统一平台处理大公司的实时数据.所以 必须具有如下特性: 支持海量数据 高吞吐量 低延迟(实时性) 支持分区,分布式 容错 2.持久化 kafka 高度依赖 文件系 ...
- 消费阿里云日志服务SLS
此文档只关心消费接入,不关心日志接入,只关心消费如何接入,可直接跳转到[sdk消费接入] SLS简介 日志服务: 日志服务(Log Service,简称 LOG)是针对日志类数据的一站式服务,在阿里巴 ...
- eclipse升级Android SDK Tool版本到25.2.5后运行项目报错Unable to build: the file dx.jar was not loaded from the SDK folder
概述 由于最近通过SDK-Manager更新了build-tools,当要用到dx.jar这个包时,自动调用最新版本Android SDK build-tools中dx.jar,但是运行android ...
- Spring入门(二):自动化装配bean
Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...
- Django-restframework 之权限源码分析
Django-restframework 之权限源码分析 一 前言 上篇博客分析了 restframework 框架的认证组件的执行了流程并自定义了认证类.这篇博客分析 restframework 的 ...
- js中let和var的区别 不懂得加QQ 2270312758
js中使用let定义变量的时候,是需要使用严格模式的,我看到网上有的博客说:如果在不使用严格模式的情况下,使用let会报错,但是在实验的过程中,我直接定义了let变量而且也没有使用严格模式,并没有报任 ...