3n+1奇偶变换

来源:互联网 发布:星际皆知你爱我txt百度 编辑:程序博客网 时间:2024/05/03 12:03

有时数据范围比较大,但是不是很确定自己计算的时间复杂度是否正确,可以写发暴力测试下

#include<stdio.h>
#define LL long long
int main()
{
    LL n;
    while(scanf("%lld",&n))
    {
        LL num=0;
        while(n!=1)
        {
            if(n%2)
                n=3*n+1;
            else
                n/=2;
            num++;
        }
        printf("%lld\n",num);
    }
    return 0;
}

0 0
原创粉丝点击