HDU 1014 Uniform Generator(最大公约数,周期循环)

#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(void)
{
    int s,m,i;
    /*int seed=0;
    int a[10005]={0};
    //memset(a,0,sizeof(a));*/
    while(scanf("%d%d",&s,&m)!=EOF);
    {
        int seed=0;
        int a[10005]={0};
        while(!a[seed])//
        {
            a[seed]=1;//若本位0,变换标志位为1,直到出现循环到头,标志过了的就跳出
            seed=(seed+s)%m;//seed根据随机数公式变化
        }
        for(i=0;i<m;i++)//范围0-m
        {
            if(a[i]==0)//只要有没出现在0~m-1的数就不好
            {
                printf("%10d%10d    Bad Choice\n\n",s,m);
                break;
            }
        }
        if(i==m){ //跳出循环后,刚好以m为周期
            printf("%10d%10d    Good Choice\n\n",s,m);
        }
    }
}

  

#include<iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int gcd(int a,int b)
{
    if(a<b)
        swap(a,b);
    return b?gcd(b,a%b):a;
}
int main()
{
    int i,j,s,m;
    while(~scanf("%d%d",&s,&m))
    {
        if(gcd(s,m)==1)
        {
            printf("%10d%10d    Good Choice\n\n",s,m);
        }
        else
        {
            printf("%10d%10d    Bad Choice\n\n",s,m);
        }
    }
    return 0;
}

  

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int step,mod,seed,count;
  while(scanf("%d%d",&step,&mod)!=EOF)
  {
    seed=0,count=1;
    do
    {
      seed=(seed+step)%mod;
      count++;                 
    }while(seed!=0);  
    count--;
    printf("%10d%10d    ",step,mod);
    if(count==mod)
      printf("%s\n","Good Choice"); 
    else
      printf("%s\n","Bad Choice"); 
    printf("\n");                                  
  }
  system("pause");
  return 0;    
}

  

#include<iostream>
using namespace std;

int main()
{
    int s,m,x,c;
    while(scanf("%d%d",&s,&m)!=EOF)
	{
        x=c=0;
        do
		{
            x=(x+s)%m;
            ++c;
        } while(x!=0);

        if(c==m)
            printf("%10d%10d    Good Choice\n\n",s,m);
        else
            printf("%10d%10d    Bad Choice\n\n",s,m);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/Roni-i/p/7198881.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值