100, express delivery 100
Source: https://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 3 & page = show_problem & problem = 36
Background
Problems in Computer Science are often classified as belonging to a certain class of problems (e.g ., NP, Unsolvable, Recursive ). in this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.
The Problem
Consider the following algorithm:
1. input n
2. printN
3. ifN= 1 then STOP
4. ifNIs odd then
5. else
6. GOTO 2
Given the input 22, the following sequence of numbers will 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 will terminate (when a 1 is 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 integersNSuch that 0 <N<1,000,000 (and, in fact, for more numbers than this .)
Given an inputN, It is possible to determine the number of numbers printed (including the 1). For a givenNThis is calledCycle-lengthOfN. In the example above, the cycle length of 22 is 16.
For any two numbersIAndJYou are to determine the maximum cycle length over all numbersIAndJ.
The Input
The input will consist of a series of pairs of integersIAndJ, One pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
You shoshould process all pairs of integers and for each pair determine the maximum cycle length over all integers between and includingIAndJ.
You can assume that no operation overflows a 32-bit integer.
The Output
For each pair of input integersIAndJYou shoshould outputI,J, And the maximum cycle length for integers between and includingIAndJ. These three numbers shocould be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integersIAndJMust appear in the output in the same order in which they appeared in the input and shocould be followed 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
Problem-solving analysis: Based on the Problem description, we need to find the longest Loop Length in the given interval.
First, what is this function?
Function f (n) is a piecewise function: WHEN n is an odd number, f (n) = 3n + 1; when n is an even number, f (n) = n/2;
What is the cycle length?
It is the number of internal operations performed by f (n) before 1 is obtained.
The Code is as follows:
(1) write the cycle length of function compute;
(2) because the question does not describe the relationship between I and j, and the question requires that the output I, j order be the same as the input I and j order, therefore, there are two methods:
First, input I, j, and then output directly, and then calculate the longest cycle length;
Type 2: Input I, j, and declare a new variable for use. The value of I and j is not affected during the calculation. When the final output is I, j can directly output it.
Code passed:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 6 int length(int n) { 7 int len=1; 8 while(n!=1) 9 {10 if(n%2==1)11 n=3*n+1;12 else13 n=n/2;14 len=len+1;15 }16 return len;17 }18 19 int main(void) {20 int start,over;21 int s,o;22 int ans;23 while(~scanf("%d%d",&start,&over))24 {25 ans=1;26 s=start, o=over ;27 if(s>o) swap(s,o);28 for(int i=s;i<=o;i++)29 {30 int len=length(i);31 ans=max(ans,len);32 }33 printf("%d %d %d\n",start,over,ans);34 }35 }