求助自己电脑上没问题提交上就超时

B2077 角谷猜想

I_AK_IOI_NM @ 2024-01-21 12:22:30

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

by STLvector @ 2024-01-21 12:28:18

int n; 换成 long long n;


by huangzhixia @ 2024-01-21 12:29:16

@xiaoxiaoluban 不开long long见祖宗

验证码:zctj祭ctj(抄题解)


by I_AK_IOI_NM @ 2024-01-21 12:42:12

@huangzhixia @STLvector 谢谢


by I_AK_IOI_NM @ 2024-01-21 12:43:09

@huangzhixia @STLvector 但第三个还是WA


by xiaoshumiao @ 2024-01-21 12:57:47

@xiaoxiaoluban 如果输入 1 ,你的程序就不对了。把判断结束挪到最前面。像这样:

while(1)
    {
     if(n==1)
        {
            cout<<"End";
            break;
        }
        if(n%2==0) 
        {
            cout<<n<<"/2="<<n/2<<endl;
            n=n/2;
        }
        else 
        {
            cout<<n<<"*3+1="<<n*3+1<<endl;
            n=3*n+1;
        }

by STLvector @ 2024-01-21 12:58:07

@xiaoxiaoluban

Input:

1

Expected Output:

End

Yours:

1*3+1=4
4/2=2
2/2=1
End

注意 1 不需要做处理。

if(n==1)
{
    cout<<"End";
    break;
}

换到循环最前面


by I_AK_IOI_NM @ 2024-01-21 13:01:05

@xiaoshumiao @STLvector 谢了


by yangjinxuan123456 @ 2024-01-28 16:13:22

只能用cout做,不能printf


by chht_0219 @ 2024-01-28 16:21:36

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

|