3n + 1 data chain Time Limit: 1000 ms Memory limit: 65536 K any questions? Click Here ^_^ In computer science, there are many similar problems that cannot be solved. However, in many cases, we do not know which type of problem can be solved, and which type of problem cannot be solved. Now we have the following problem:
(1) enter a positive integer n;
(2) display n;
(3) end if n = 1;
(4) If n is an odd number, then n becomes 3 * n + 1, otherwise it becomes n/2;
(5) Transfer to step (2.
For example, for the input positive integer 22, the following series should be displayed:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
We speculate that for any positive integer, after the above algorithm, it will eventually be pushed to 1. Although this algorithm is simple, we still cannot determine whether our inference is correct. Fortunately, we have a computer and we have verified that all positive integers smaller than 1000000 satisfy the above inference.
For a given positive integer n, we define the number of displayed numbers as the chain length of n. For example, the chain length of 22 is 16.
Your task is to write a program. For any positive integer I and j, the maximum chain length between I and j is given, of course, the longest chain length is generated by a positive integer between I and j. Here I and j include I and j. Input two integers I, j, I, and j are separated by a space. 0 <I <= j <10000. The output data has only one row, that is, the longest chain length between I and j. Sample Input
1 10
Sample output
20
# Include
Int getLinkLen (int n)
{
If (n = 1) return 1;
Return n & 1? GetLinkLen (n * 3 + 1) + 1: getLinkLen (n/2) + 1;
}
Int main ()
{
Int x, y, I, t, max = 0;
Scanf ("% d", & x, & y );
For (I = x; I <= y; I ++ ){
T = getLinkLen (I );
If (t> max) max = t;
}
Printf ("% d \ n", max );
Return 0;
}