2014-06-11 13:59:56
题意&思路:简单题不多说。
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> using namespace std; int main(){ int s[10005],p[10005],Z,I,M,L,Case = 0; while(scanf("%d %d %d %d",&Z,&I,&M,&L) == 4){ if(Z == 0 && I == 0 && M == 0 && L == 0) break; memset(s,0,sizeof(s)); memset(p,0,sizeof(p)); s[L] = 1; p[L] = 1; for(int cnt = 2; ; ++cnt){ L = (Z * L + I) % M; if(s[L]){ printf("Case %d: %d\n",++Case,cnt - p[L]); break; } else{ s[L] = 1; p[L] = cnt; } } } return 0; }
本文介绍了一道关于求解循环周期的简单题,通过使用C++编程语言实现了该问题的算法。具体方法为记录每次循环的状态,一旦发现状态重复,则可以确定循环周期。
152

被折叠的 条评论
为什么被折叠?



