题目
Uniform Generator
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22744 Accepted Submission(s): 8944
seed(x+1) = [seed(x) + STEP] % MOD
where '%' is the modulus operator.
Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of this form is that they will always generate the same pattern over and over. In order to minimize this effect, selecting the STEP and MOD values carefully can result in a uniform distribution of all values between (and including) 0 and MOD-1.
For example, if STEP = 3 and MOD = 5, the function will generate the series of pseudo-random numbers 0, 3, 1, 4, 2 in a repeating cycle. In this example, all of the numbers between and including 0 and MOD-1 will be generated every MOD iterations of the function. Note that by the nature of the function to generate the same seed(x+1) every time seed(x) occurs means that if a function will generate all the numbers between 0 and MOD-1, it will generate pseudo-random numbers uniformly with every MOD iterations.
If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (or any other repeating series if the initial seed is other than 0). This is a poor selection of STEP and MOD because no initial seed will generate all of the numbers from 0 and MOD-1.
Your program will determine if choices of STEP and MOD will generate a uniform distribution of pseudo-random numbers.
3 5 15 20 63923 99999
3 5 Good Choice 15 20 Bad Choice 63923 99999 Good Choice
使用控制符时,在程序开头加投文件#include <iomanip>
dec 设置基数为10
hex 设置基数为16
oct 设置基数为8
setfill(c) 设置填充字符c
setprecision(n) 设置显示小数精度为n位
setw(n) 设置域宽为n个字符
setiosflags(ios::fixed) 固定的浮点显示
setiosflags(ios::scientific) 指数表示
setiosflags(ios::left) 左对齐
setiosflags(ios::right) 右对齐
setiosflags(ios::skipws) 忽略前导空白
setiosflags(ios::uppercase) 16进制数大写输出
setiosflags(ios::lowercase) 16进制小写输出
2、用流对象的成员函数 格式控制符:
可以不使用#include<iomanip>的
cout.precision()设置小数点后精确度,
cout.width()设置宽度,
cout.setf()设置显示格式,比如
cout.setf(ios::left)左对齐
cout.setf(ios::showpoint)不管是否有小数位,显示小数点
cout.fill();不足宽度则填充,如cout.fill('0');
如这次周赛1002,如果使用COUT在输出前要这样设置一下。
cout.precision(6);
cout.width(8);
cout.setf(ios::left);
cout.setf(ios::showpoint);
cout.fill('0');
下面我再来说这个题,这个题就是求a 、b是不是互质。若能看出来这个,这题就是非常简单了
下面贴出我得ac代码
#include <iostream>
#include <iomanip>
using namespace std;
//hdu-oj-1014
//判断输入的两个数是否互质,若互质则good choice
//否则bad choice
bool gcd(int a,int b)
{
if(a%b==0)
{
if(b==1)
return true;
else
return false;
}
else
return gcd(b,a%b);
}
int main()
{
int a,b;
while(cin>>b>>a)
{
cout<<setw(10)<<std::fixed<<b<<setw(10)<<std::fixed<<a<<" ";
if(gcd(a,b))
cout<<"Good Choice"<<endl;
else
cout<<"Bad Choice"<<endl;
cout<<endl;
}
return 0;
}