3964 - C.产生均匀分布的随机数据
模拟计算中通常需要一些随机数,一种产生随机数据的方法可通过下式得到:
si+1 = (si + step)%mod
式中的“%”表示求余操作符,利用该表达式可产生[0, mod-1]范围内的随机数。
Input
每一行包含两个整型数据,分别表示step(step >=1)和mod(mod <=105)两个参数
Output
对应于输入参数的每一行,在第1~10列位置上以右对齐方式打印参数step的值,在第11~20列位置上以右对齐方式打印参数mod的值,接着在第25列开始以左对齐方式打印“Good Choice”或者“Bad Choice”。
Examples
Input
3 5 15 20 63923 99999
Output
3 5 Good Choice
15 20 Bad Choice
63923 99999 Good Choice
Solution C
#include<stdio.h> int main( int argc, char ** argv ) { int Step, Mod; while( scanf( "%d%d", &Step, &Mod ) != EOF ) { int Book[100001] = { 0 }; int Count = 0, S = 0; while( Book[S] == 0 ) { Book[S] = 1; S = ( S + Step) % Mod; Count++; } if( Count == Mod ) printf( "%10d%10d Good Choice\n", Step, Mod ); else printf( "%10d%10d Bad Choice\n", Step, Mod ); } return 0; }
Solution C++
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; int f[100000]; int main() { int step, mod; // step,mod int i, a; bool tag; while (cin >> step >> mod) { memset(f, 0, sizeof(f)); tag = true; a = 0; f[a]++; for (i = 1; i < mod; i++) { a = (a + step) % mod; f[a]++; if (f[a] >= 2) { tag = false; break; } } printf("%10d%10d ", step, mod); // 打印10位数 if (tag) cout << "Good Choice" << endl; else cout << "Bad Choice" << endl; } return 0; }