Generate random numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 114 Accepted Submission(s): 44
Problem Description
John von Neumann suggested in 1946 a method to create a sequence of pseudo-random numbers. His idea is known as the "middle-square"-method and works as follows: We choose an initial value a0, which has a decimal representation of length at most n. We then multiply the value a0 by itself, add leading zeros until we get a decimal representation of length 2 × n and take the middle n digits to form ai. This process is repeated for each ai with i>0. In this problem we use n = 4.
Example 1: a0=5555, a02=30858025, a1=8580,...
Example 2: a0=1111, a02=01234321, a1=2343,...
Unfortunately, this random number generator is not very good. When started with an initial value it does not produce all other numbers with the same number of digits.
Your task is to check for a given initial value a0 how many different numbers are produced.
Input
The input contains several test cases. Each test case consists of one line containing a0 (0 < a0 < 10000). Numbers are possibly padded with leading zeros such that each number consists of exactly 4 digits. The input is terminated with a line containing the value 0.
Output
For each test case, print a line containing the number of different values ai produced by this random number generator when started with the given value a0. Note that a0 should also be counted.
Sample Input
5555
0815
6239
0
Sample Output
32
17
111
Hint
Note that the third test case has the maximum number of different values among all possible inputs.
Source
Recommend
lcy
解题报告:这道题主要是题意不太好懂,今天内部测试的时候花了很长时间才看懂,哎!英语不行呀,这道题就是给我们四位数(形式上是四位,可以有前导零例如:0021),让它产生随机数,方法是让其平方后为形式上的八位数例如:0021平方后为00000441去中间的四位0004,依此类推,终究会有一个四位数和前面的一样,就是求这个数的位置;
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1010;
int a[N], a2[N];//a[]存储的是“形式上”的四位数,a2[]存储的是“形式上”的八位数
int main()
{
int n, i, ans , j, flag;
while (scanf("%d", &n) != EOF && n)
{
memset(a, 0, sizeof(a));
memset(a2, 0, sizeof(a2));
a[0] = n;
for (i = 0; i < N; ++i)
{
a2[i] = a[i] * a[i];
a[i + 1] = a2[i] / 100 - (a2[i] / 1000000) * 10000; //取中间的四位数
}
flag = 0;
for (i = 0; i < N && !flag; ++i)
{
for (j = i + 1; j < N && !flag; ++j)
{
if (a[i] == a[j])//找到第一个四位数和之前的一样
{
flag = 1;
ans = j;//记录这个数的位置
}
}
}
printf("%d\n", ans);
}
return 0;
}