Difficulty of this question: easy
1. Topics
Callatz conjectures:
For any positive integer n, if it is even, then cut it in half; if it is odd, then cut (3n+1) in half. This has been cut down repeatedly, and finally n=1 must be obtained at a certain step. Karaz announced this conjecture at the World Congress of Mathematicians in 1950. It is said that the teachers and students of Yale University mobilized to prove this seemingly silly and naive proposition. +1), so that some people say that this is a conspiracy, that Karaz is deliberately slowing down the progress of teaching and scientific research in the American mathematics community...
Our topic today is not to prove the Karaz conjecture, but for any given positive integer n not exceeding 1000, simply count how many steps (cuts) are needed to get n=1?
Input format:
Each test input contains 1 test case, i.e. given the value of a positive integer n.
Output format:
Output the number of steps needed to compute from n to 1.
Input sample:
3
Sample output:
5
code length limit
16 KB
time limit
400 ms
memory limit
64 MB
2. Problem-solving ideas
1. Read in the input data n
2. Declare a variable step count
3. Use the while loop to perform corresponding operations on n
4. Print the value of step
3. Code
#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int step = 0;
while(n!=1){
if(n%2==0){
n=n/2;
}else{
n=(3*n+1)/2;
}
step++;
}
printf("%d\n",step);
}