PAT乙级 1001 害死人不偿命的(3n+1)猜想 C++

//对于一个任意的不超过1000的正整数,偶数除以二,奇数(3*n+1)除以2,到多少步才可以使得n=1
#include <iostream>

int main(int argc,const char* argv[])
{
int n,cnt;
cnt = 0;
std::cin >> n;
while(n != 1)
{
if (0 == n)
{
return 1;
}
else if (0 == n%2)
{
n = n/2;
cnt++;
}
else
{
n = (3*n + 1)/2;
cnt++;
}
}
std::cout << cnt;
return 0;
}

 

posted @ 2022-07-15 14:16  九饼多一点  阅读(25)  评论(0)    收藏  举报