1 条题解

  • 0
    @ 2023-6-11 12:16:29

    C :

    #include<stdio.h>
    
    void main(){
        int n,c=0;
        scanf("%d",&n);
    
        while(n!=1){
            if(n%2==0){
                n=n/2;
            }else{
                n=n*3+1;
            }
            c++;
        }
        printf("%d",c);
    }
    

    C++ :

    #include<stdio.h>  
    int main()  
    {  
        int n;  
        scanf("%d", &n);  
        int c = 0;
        while (n != 1)  
        {  
            if (n % 2 == 1)  
            {  
                int sum = n * 3 + 1;  
                //printf("%d*3+1=%d\n", n,sum);  
                c++;
                n = sum;  
            }  
            else  
            {  
                int div = n / 2;  
                //printf("%d/2=%d\n", n, div);  
                c++;
                n = div;  
            }  
        }  
        
        printf("%d\n", c);  
        return 0;  
      
    }  
    

    Python :

    n = int(input())
    c = 0
    while True:
        if n % 2 == 0:
            n = n // 2
        else:
            n = n * 3 + 1
        c += 1
        if n == 1:
            break
    print(c)
    
    • 1

    信息

    ID
    2168
    时间
    1000ms
    内存
    16MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者