您的位置:首页 > 其它

杭电oj 1014

2015-12-08 21:30 387 查看
题目

Uniform Generator

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 22744 Accepted Submission(s): 8944



[align=left]Problem Description[/align]
Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form

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.

[align=left]Input[/align]
Each line of input will contain a pair of integers for STEP and MOD in that order (1 <= STEP, MOD <= 100000).

[align=left]Output[/align]
For each line of input, your program should print the STEP value right- justified in columns 1 through 10, the MOD value right-justified in columns 11 through 20 and either "Good Choice" or "Bad Choice" left-justified starting in
column 25. The "Good Choice" message should be printed when the selection of STEP and MOD will generate all the numbers between and including 0 and MOD-1 when MOD numbers are generated. Otherwise, your program should print the message "Bad Choice". After each
output test set, your program should print exactly one blank line.

[align=left]Sample Input[/align]

3 5
15 20
63923 99999


[align=left]Sample Output[/align]

3         5    Good Choice

15        20    Bad Choice

63923     99999    Good Choice


由于这题要求输出格式,在这里我给大家普及一下输出的格式问题
C++有两种方法控制格式输出:1、用格式控制符;2、用流对象的成员函数 格式控制符。
1 、I/O流常用控制符:
使用控制符时,在程序开头加投文件#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: