题目描述:编写一程序,验证角谷猜想。所谓的角谷猜想是:“对于任意大于1的自然数n,若n为奇数,则将n变为3*n+1,否则将n变为n的一半。经过若干次这样的变换,一定会使n变为1。”

样例

10

输出

6

具体代码如下:

#include <bits/stdc++.h>
using namespace std;
#define N 1005
int x[N];

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

    return 0;
}
 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐