TCl求助

B2077 角谷猜想

yunya @ 2024-08-26 17:23:18



#include<math.h>

void g(int n){

  if(n%2==0){

    printf("%d/2=%d\n",n,n/2);
  }
  else{
    printf("%d*3+1=%d\n",n,n*3+1);
  }
}

int f(int n){
  if(n%2==0){
    n=n/2;
  }
  else{
    n=n*3+1;
  }
  return n;
}
int main(){
   int n;
   scanf("%d",&n);
   while(n!=1){
    g(n);
    n=f(n);
   }
   printf("End"); 
    return 0;
    }
    、、、、、、
 请大家帮忙看看为啥会时间超限吧,不太明白为什么。

by hhztl @ 2024-08-26 17:27:16

@yunya 十年OI一场空,不开long long见祖宗


by chenhaihang @ 2024-08-26 17:37:03

@hhztl 他是超时诶和longlong啥关系


by hhztl @ 2024-08-26 17:38:32

@chenhaihang 因为数字爆int后会变成负数


by yunya @ 2024-08-26 22:45:43

@hhztl 把数据类型换为Long long类型之后,问题果然解决了


by yunya @ 2024-08-26 22:46:10

@hhztl 原来是这样


by lawhello @ 2024-08-28 12:30:40

@yunya 哥们,输出也用%lld吧,要不然过不了子任务


by chenzhishuo2012 @ 2024-09-04 13:44:27

@yunya 求互关

#include<bits/stdc++.h>
using namespace std;
long long n;
int main(){
    cin>>n;
    while(n!=1){
        if(n%2==1){
            cout<<n<<"*3+1="<<n*3+1<<endl;
            n=n*3+1;
        }
        else{
            cout<<n<<"/2="<<n/2<<endl;
            n/=2;
        }
    }
    cout<<"End"<<endl;
    return 0;
}

by tuonisitake @ 2024-09-25 23:54:10

#include <stdio.h>

int main()
{
    long long n;
    scanf("%lld",&n);
    while (n!=1)
    {
        if(n%2==0)
        {
            printf("%lld/2=",n);
            n/=2;
            printf("%lld\n",n);
        }

        else if(n%2!=0){
            printf("%lld*3+1=",n);
            n=n*3+1;
            printf("%lld\n",n);
        }
    }
    printf("End");
    return 0;
}

|