PTA_乙级_1001_C++

PTA_乙级_1001_C++_第1张图片
思路:使用判断语句即可,使用while进行循环,终止条件是n不等于1,然后用if-else判断奇数偶数

#include 
using namespace std;

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

你可能感兴趣的:(PTA乙级,c++,开发语言)