3964 - C.产生均匀分布的随机数据
时间限制 : 1 秒
内存限制 : 128 MB
模拟计算中通常需要一些随机数,一种产生随机数据的方法可通过下式得到:
si+1 = (si + step)%mod
式中的“%”表示求余操作符,利用该表达式可产生[0, mod-1]范围内的随机数。
题目输入
每一行包含两个整型数据,分别表示step(step >=1)和mod(mod <=105)两个参数
题目输出
对应于输入参数的每一行,在第1~10列位置上以右对齐方式打印参数step的值,在第11~20列位置上以右对齐方式打印参数mod的值,接着在第25列开始以左对齐方式打印“Good Choice”或者“Bad Choice”。
输入/输出样例
输入格式
3 5 15 20 63923 99999
输出格式
3 5 Good Choice
15 20 Bad Choice
63923 99999 Good Choice
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; }
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; }
Java解答
import java.lang.reflect.Array; import java.util.Arrays; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()){ int step =in.nextInt(); int mod =in.nextInt(); boolean[] flag = new boolean[mod]; Arrays.fill(flag, false); int step1 = 1; boolean B = true; for(int i=0;i<mod;i++){ step1 =(step1+step)%mod; if(flag[step1]==false) flag[step1] = true; else{ B=false; break; } } if(B == false){ System.out.printf("%10d%10d",step,mod); System.out.println(" "+"Bad Choice"); } else{ System.out.printf("%10d%10d",step,mod); System.out.println(" "+"Good Choice"); } } } }