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 并标记出现,直至重复出现时跳出,输出长度。
解题思路:用一个数组记录 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; }
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?