uva350(数论)

题意:求循环的长度。。

思路:一开始想用结构体来做,没得到一个L就flag一下表示访问过,不过输入数据就卡住了不得不得不放弃。

后来用了下面这种方法感觉是一样的就是测试数据过不了,有时间在改改试试。。感觉代码1,代码2思路是一样的。不知道错在哪里了。。。

代码1:未过测试数据。。哦原来是数组开小了。。代码1:还有个问题就是But be careful: the cycle might not begin with the seed!数组不一定是从第一个数组开始的所以导致第三组测试数据输出501。。。

 

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[1010];
int main()
{
 int Z, I, M,L;
 while(scanf("%d%d%d%d",&Z,&I,&M,&L)!=EOF)
 {
  if(Z==0&& I==0 &&M==0&& L==0)
   break;
  int count=0;
  memset(a,0,sizeof(a));
  while(1)
  {
   if(a[L]==0)
   {
    a[L]=1;
    count++;

   }
   L=(Z*L+I)%M;
   if(a[L]!=0)
    break;
  }
  printf("%d\n",count);
 }
 return 0;
}

 

//代码2:AC
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=10100;
int a[maxn];//数组开大点
int main()
{
 int Z, I, M,L;
 int flag=1;
 while(scanf("%d%d%d%d",&Z,&I,&M,&L)!=EOF)
 {
  if(Z==0&& I==0 &&M==0&& L==0)
   break;
  memset(a,0,sizeof(a));
  int count=0;
  do
  {
   L=(Z*L+I)%M;
   a[L]++;
   count++;
  }while(a[L]==1);
  printf("Case %d: %d\n",flag++,count-1);
 }
 return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值