PATB1001 The (3n+1) Conjecture of Killing People

insert image description here

Inscription

This problem is a simple simulation. If it is a data input, then a simple loop can be implemented, but if the amount of data is large, the efficiency of the algorithm will be extremely low, so pruning can be used.

code show as below

#include <iostream>
using namespace std;

int n;

int main()
{
    
    
    int count=0;//记步数
    cin>>n;
    while(n!=1){
    
    
        if(n%2==0)
            n=n/2;
        else
            n=(3*n+1)/2;
        count++;
    }
    cout<<count;

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518693&siteId=291194637