3n+1问题

//3n+1问题(有bug)
#include<stdio.h>

int main()
{
    int n,count=0;
    scanf("%d",&n);
    while(n>1)
    {
        if(n%2==0)
        {
            n/=2;
        }
        else
        {
            n=3*n+1;
        }
        count++;
    }
    printf("%d\n",n);
    return 0;
}
//3n+1问题(改进版)
#include<stdio.h>

int main()
{
    int n1,count=0;
    scanf("%d",&n1);
    long long n=n1;
    while(n>1)
    {
        if(n%2==0)
        {
            n/=2;
        }
        else
        {
            n=3*n+1;
        }
        count++;
    }
    printf("%d\n",n);
    return 0;
}

你可能感兴趣的:(算法)