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

Subject description:

Kharazi (Callatz) guess:

For any positive integer 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 conjecture, but on any given positive integer not exceeding 1,000 n, simply count the number, how many steps (cut a few) to get n = 1 need?

Input formats:

Each test comprises a test input, i.e., the value given positive integer n.

Output formats:

N is calculated from the output to a desired number of steps.

Sample input:

3

Sample output:

5

Problem-solving ideas

The subject can be seen, it is even or odd is determined for different treatments, steps ++

Code

#include<iostream>
using namespace std;
int main() {
    int n;
    cin>>n;
    int count=0;     //步数
    while(n!=1) {
        if(n%2==0) { //偶数
            n /= 2;
        }
        else {       //奇数
            n = (3*n+1)/2;
        }
        count++;
    }
    cout<<count<<endl;
    return 0;
}
Published 55 original articles · won praise 30 · views 9814

Guess you like

Origin blog.csdn.net/chaifang0620/article/details/104852605