Zhejiang University PAT-1001 The (3n+1) Conjecture of Killing People Without Paying for Life (15 points/15 points)

Callatz conjecture:

For any positive integer n, if it is even, cut it in half; if it is odd, cut (3n+1) in half. It keeps cutting down repeatedly, and finally n=1 at a certain step. Karaz announced this conjecture at the World Congress of Mathematicians in 1950. According to legend, teachers and students of Yale University were mobilized at that time, desperately trying to prove this seemingly silly and naive proposition. As a result, the students were indifferent to their studies. +1), so that some people say that this is a conspiracy, Karaz is deliberately delaying the progress of teaching and scientific research in American mathematics...

Our topic today is not to prove Karaz’s conjecture, but to simply count any given positive integer n not exceeding 1000. How many steps (cut several times) does it take to get n=1?

Input format:

Each test input contains 1 test case, which gives the value of a positive integer n.

Output format:

Output the number of steps required to calculate from n to 1.

Input sample:

3

Sample output:

5

 

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int num,count=0;
    while(scanf("%d",&num)!=1);
    while(num!=1){
        if(num%2==0)        //偶数
            num = num/2;
        else                 //奇数
            num = (3*num+1)/2;
        count ++;
    }
    printf("%d\n",count);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_33514421/article/details/106279320