PAT Class B true question 1001. The dead are not worth the candle (3n + 1) conjecture (15) (Problem Solving), pat3n
Callatz conjecture:
For any natural number n, if it is an even number, cut it by half; if it is an odd number, cut (3n + 1) by half. In this way, we will continue to cut down and finally get n = 1 in a certain step. Karaz announced this conjecture at the world mathematician conference in 1950. It is said that at that time, the teachers and students of Yale University were mobilized to prove this seemingly silly and naive proposition, and the results made the students unwilling to study, as a result, some people say this is a conspiracy. karaz is deliberately delaying the progress of teaching and scientific research in American mathematics ......
Today's question is not to prove caraz's conjecture, but to give a given positive integer n of no more than 1000, simply count it. How many steps (cut down) Do we need to get n = 1?
Input Format: Each test input contains one test case, that is, the value of natural number n.
Output Format: Number of steps required for the output from n to 1.
Input example: 3
Output example: 5
Code:
1 # include <stdio. h> 2 int main () 3 {4 int n = 0; // receives the n of the pre-judgment; 5 int I = 0; // saves the number of calculated steps; 6 scanf ("% d", & n); 7 while (n! = 1) // if 1 is not obtained, it is cut all the time; 8 {9 if (n % 2 = 0) 10 {11 n/= 2; 12} 13 else14 {15 n = (3 * n + 1)/2; 16} 17 I ++; 18} 19 printf ("% d", I ); 20 return 0; 21}
/* Compile the environment ********** vc ++ 6.0 (32-bit )*************/