B1001 killed attractiveness of the (3n + 1) guess

Title Description

  Kharazi (Callatz) guess:

  For any natural number n, if it is even, then it halved; if it is odd, the (3n + 1) cut by half. This has been repeatedly cut, finally got to get n = 1 in a step. Kharazi at the 1950 World Congress of Mathematicians announced this conjecture, it was said that Yale students and teachers Qidong Yuan, desperately want to prove this seemingly absurd clear title, leaving students inadvertently studies, as one proof (3n + 1)

  Given a positive integer n 1000 does not exceed, and simply count the number, how many steps required to get n = 1

Input Format

Each test input comprises a test case, i.e. given natural number n

Output Format

Output calculation from n to 1 to the number of steps required

SAMPLE INPUT

3

Sample Output

5

The basic idea

  1. An open loop, loop condition is not an input digital
  2. When this number is even, divided by 2
  3. When this number is odd, is multiplied by 1 plus 3 for operation result is divided by 2
int n, step = 0; 
scanf("%d", &n);
while(n != 1){
    if(n % 2 == 0){
        n /= 2;     
    }else{
        n = (3*n + 1) / 2;
    }
    step++;
}    
printf("%d\n", step);    

  

Guess you like

Origin www.cnblogs.com/YC-L/p/12089787.html