超时了

B2077 角谷猜想

BinSky @ 2023-04-07 20:06:08

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

超时了,帮忙看下怎么改?


by VitrelosTia @ 2023-04-07 20:07:57

@shixuanbin 你需要开 longlong

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

by Light_az @ 2023-04-07 20:08:29

@shixuanbin 开 long long


by BinSky @ 2023-04-07 20:13:07

过了,谢谢啊


by heyx0201 @ 2023-08-04 20:49:36

@shixuanbin 不开 long long 有时候会 TLE


|