PAT B1001 害死人不偿命的(3n+1)猜想

简单的模拟,while n=1 位临界条件
在这里插入图片描述

#include <iostream>
#include <algorithm>
#include <string>
int main(){
    int step=0,n;
    scanf("%d",&n);
    while(n!=1){
        if(n%2==0)
        {
            n=n/2;
        }
        else
        {
            n=(3*n+1)/2;
        }
        step++;
    }
    printf("%d",step);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Decmxj1229/article/details/88358391