什么是随机数据:
emm…字面含义。
制作随机数据:
首先,你要用这个:
srand(time(0));//生成数据的由时间定,由于你运行的时间不同,你生成的数据也不会相同。
接着你就可以生成随机数据了。
如:
srand(time(0));
int a,b;
a=rand()%100,b=rand()%100;//将 a,b 赋予 0~99 之间的随机值
举个例子
A+B problem:
变化数据范围
数据范围: 1 ≤ a , b ≤ 1 0 9 1\le a,b \le 10^9 1≤a,b≤109
代码:
制造输入数据的:
#include<bits/stdc++.h>
using namespace std;
const int ab=1e9;//数据范围
int main()
{
freopen("day1.in","w",stdout);//数据被传到了day1.in 里面了,你要先新建一个文本文件
srand(time(0));
cout<<rand()%ab+1<<" "<<rand()%ab+1<<endl;//将随机数据传入
return 0;
}
制造输出数据的:
#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("day1.in","r",stdin);//提取
freopen("day1.out","w",stdout);//传入
int a,b;
cin>>a>>b;
cout<<a+b<<endl;
return 0;
}
数据范围: 0 ≤ a , b ≤ 1 0 9 0\le a,b \le 10^9 0≤a,b≤109
#include<bits/stdc++.h>
using namespace std;
const int ab=1e9;
int main()
{
freopen("day1.in","w",stdout);
srand(time(0));
cout<<rand()%ab<<" "<<rand()%ab<<endl;//相较之前,只要改这一行
return 0;
}
A-B problem:
1 ≤ b ≤ a ≤ 1 0 9 1 \le b \le a \le 10^9 1≤b≤a≤109
#include<bits/stdc++.h>
using namespace std;
const int ab=1e9;
int main()
{
freopen("day1.in","w",stdout);
srand(time(0));
int a,b;
a=rand()%ab+1,b=rand()%ab+1;
while(a<b)
a=rand()%ab+1,b=rand()%ab+1;//一直重新刷数据,直到 满足要求就会弹出
cout<<a<<" "<<b<<endl;
return 0;
}
类似的,你可以写出所有的随机数据代码(只要有标程)。
随机字符串(大写):
#include<bits/stdc++.h>
using namespace std;
const int l=1e3;//随机字符串的长度范围,这里以1e3为例
int main()
{
srand(time(0));
int len=rand()%l;//随机长度
for(int i=0;i<len;i++){
char c=0;
while(c<'A'||c>'Z'){
int a=rand()%42+1;
c=char(a+47);
}
cout<<c;
}
printf("\n");
return 0;
}