题目大意:给出两个数 ,s,m,按照题目中的要求计算生成的随机数列,判断是否生成了0~m-1这m个数列,若是则是好的选择,否则是坏的选择。
算法思想;
用一个标记flag[MAXN]来记录前0~m-1是否生成,同时用num记录数列的长度,循环判断当前的数是否先前生成过,若没有则标记,否及判断num是否等于m,若等则是一个好的选择,不等则不是一个好选择。输出时s,m均占10个字节有队齐,字符串从25列开始输出,左对齐。每个用例后空一行。
代码如下:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=111111;
bool flag[MAXN];
int main(){
int s,m;
int seed;
while(cin>>s>>m){
int num=0;
memset(flag,0,sizeof(flag));
seed=0;
while(!flag[seed]){
flag[seed]=true;
seed=(seed+s)%m;
num++;
}
if(num==m) cout<<right<<setw(10)<<s<<setw(10)<<m<<" "<<left<<"Good Choice"<<endl<<endl;
else cout<<right<<setw(10)<<s<<setw(10)<<m<<" "<<left<<"Bad Choice"<<endl<<endl;
}
return 0;
}