UVA - 350 Pseudo-Random Numbers

来源:互联网 发布:linux下删除用户 编辑:程序博客网 时间:2024/05/16 04:15

题目大意:给出 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; }
0 0
原创粉丝点击