您的位置:首页 > 其它

uva350(数论)

2013-05-30 10:58 267 查看
题意:求循环的长度。。

思路:一开始想用结构体来做,没得到一个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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: