Problem Descriptionproblems in computer science is often classified as belonging to a certain class of problems (e.g., NP , unsolvable, Recursive). In this problem you'll be analyzing a property of an algorithm whose classification are not known for all possible inputs .
Consider the following algorithm:
1. Input n
2. Print N
3. If n = 1 then STOP
4. If n is odd then n <-3n + 1
5. else N <-N/2
6. GOTO 2
Given the input, the following sequence of numbers would be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above would terminate (when a 1 was printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such 0 < n < 1,000,000 (and, in fact, for many more numbers T Han this.)
Given an input n, it's possible to determine the number of numbers printed (including the 1). For a given n called the cycle-length of N. The example above, the cycle length of is 16.
For any of the numbers I and j you is to determine the maximum cycle length through all numbers between I and J.
Inputthe input would consist of a series of pairs of integers I and J, one pair of integers per line. All integers'll is less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length to all integers between an D including I and J.
You can assume that no opperation overflows a 32-bit integer.
Outputfor each pair of input integers I and j-should output I, j, and the maximum cycle length for integers between an D including I and J. These three numbers should be separated by at least one space with all three numbers on one line and with one line of OUTP UT for each line of input. The integers I and J must appear in the output of the same order in which they appeared in the input and should is Followe D by the maximum cycle length (on the same line).
Sample Input
1 10100 200201) 210900 1000
Sample Output
1 10 20100 200 125201 210 89900 1000 174
The code is as follows:
#include <iostream>using namespace Std;int main () { int i,j,t; int N,a,b,k,max; while (cin>>i>>j) { a=i; B=j; if (I>J) //If i>j, so that I and J exchange position, convenient subsequent calculation { t=i; I=j; j=t; A=j; b=i; If a A, a transformation, then the output should pay attention to the following } max=0; for (; i<=j;i++) { k=1; n=i; Do { if (n==1) break ; if (n%2==1) n=3*n+1; else n/=2; k++; } while (1); if (max<k) max=k; } cout<<a<< "" <<b<< "" <<max<<endl; } return 0;}
Problem Solving Ideas:
The topic requirements are easy to read, given a range, in this range the number passes through a series of transformations after the output range and the maximum value within the interval
The key is the two number of the given range, which is smaller, when the output should be kept in the same order as the original
Yt14-hdu-The maximum number of transformed values in the range