超时

B2077 角谷猜想

kimi1011 @ 2024-11-09 14:28:48

为什么超时了??```

include<bits/stdc++.h>

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


by wangjiawen @ 2024-11-09 14:30:23

希丰展,使MD @kimi1011


by tengyuxuan @ 2024-11-17 10:31:13

n用long long


by tengyuxuan @ 2024-11-17 10:33:22

@kimi1011改成这样试试:

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

by tengyuxuan @ 2024-11-17 10:39:01

@kimi1011 还有else下面那个cout, cout<<n<<"3+1"<<n3+1; 字符串里面缺了个*, AC代码:

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

by tengyuxuan @ 2024-11-17 10:44:19

发代码用上面那排的</>符号,点下然后直接复制代码


by G18583301777 @ 2024-11-26 17:42:54

or

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

AC代码


|