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

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

Kharazi (Callatz) conjecture:
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, was the legendary Yale University teachers and students Qidong Yuan, desperately want to prove this seemingly silly naive proposition, students inadvertently result so much noise studies, one only card (3n +1), so some people say this is a conspiracy, Kharazi was deliberately delaying the progress of American mathematics teaching and research ......
our topic today is not evidence Minka Raz guess, but given either does not exceed 1000 positive integer n, simply count the number, how many steps (cut a few) to get n = 1 need?
Input format: Each test comprises a test input, i.e., given the value of the natural number n.
Output Format: n is calculated from the output to a desired number of steps.
Sample input:
3
Output Sample:
5

#include <stdio.h>

int time = 0;

int calculate(int thisnum) {

    if (thisnum == 1) {
        return time;
    } else if (thisnum % 2 == 0) {
        time++;
        thisnum = thisnum / 2;
        calculate(thisnum);
    } else {
        time++;
        thisnum = 3 * thisnum + 1;
        calculate(thisnum / 2);
    }

}

int main() {

    int num;
    scanf("%d", &num);
    printf("%d", calculate(num));
    return 0;
}

Test Results:
Here Insert Picture Description

Published 33 original articles · won praise 1 · views 4162

Guess you like

Origin blog.csdn.net/qq_39827677/article/details/103855179