入门第一题,格式了解ing
传送门 (3n+1)猜想
一个循环的问题
值得注意的点是:
- 奇偶数的判定
(n & 1) == 0 //按位与操作 最低位为0是偶数,否则是技术
- 取半的操作
n = n >> 1;
#include<iostream>
using namespace std;
int main()
{
int n, res = 0;
cin >> n;
while(n != 1)
{
res++;
if((n & 1) == 0) n = n >> 1;
else n = (3 * n + 1) >> 1;
}
cout << res <<endl;
return 0;
}