您的位置:首页 > 其它

UVA - 350 Pseudo-Random Numbers

2016-07-12 18:15 337 查看
题目大意:给出 Z,I,M,L,根据 L=(Z*L+I)modM 计算每一轮 L,输出循环的 L 的个数,注意循环不一定从所给的 L 开始。

解题思路:用一个数组记录 L 是否出现过,未出现循环长度 +1 并标记出现,直至重复出现时跳出,输出长度。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int num[10000];
int count = 0;
int main() {
int Z, I, M, L;
while (scanf("%d%d%d%d", &Z, &I, &M, &L) != EOF) {
memset (num, 0, sizeof(num));
if (!(Z || I || M || L)) break;
int tot = 0;
L = (Z * L + I ) % M;
while ( !num[L]) {
tot++;
num[L] = 1;
L = (Z * L + I ) % M;
}
printf("Case %d: %d\n", ++count, tot);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva