求助!25分!样例过了!!!

B2077 角谷猜想

LYH12061Oh_Dream @ 2022-08-18 14:29:52

这是我的代码:

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

by ISTP @ 2022-08-18 14:31:51

递归可能会爆栈,建议转成while循环


by LYH12061Oh_Dream @ 2022-08-18 14:32:50

好的,谢谢


by ISTP @ 2022-08-18 14:33:29

以及输出奇数时的格式错了

printf("3*%d+1=%d\n",x,3*x+1);

改成

printf("%d*3+1=%d\n",x,3*x+1);

by LYH12061Oh_Dream @ 2022-08-18 14:42:09

AC了


|