为啥全RE???

B2144 阿克曼(Ackermann)函数

@[fanboyang](/user/1286089) ```c #include<bits/stdc++.h> using namespace std; int fby( int m,int n) { if(m==0) { return n+1; } if(m>0 && n==0) { return fby(m-1,1); } if(n>0 ) { return fby(m-1,fby(m,n-1));//要加return } } int main(){ int n,m; cin>>m>>n; cout<<fby(m,n); return 0; } ```
by LHM_zs @ 2024-08-21 09:41:02


@[LHM_zs](/user/1024294) very thank you
by fanboyang @ 2024-08-21 10:36:05


@[fanboyang](/user/1286089) **求互关** ```cpp #include<bits/stdc++.h> using namespace std; int m,n; int akm(int m,int n){ if(m==0)return n+1; if(n==0)return akm(m-1,1); return akm(m-1,akm(m,n-1)); } int main(){ cin>>m>>n; cout<<akm(m,n)<<endl; return 0; } ```
by chenzhishuo2012 @ 2024-09-11 08:39:27


@[fanboyang](/user/1286089) ```cpp #include<bits/stdc++.h> using namespace std; int fby( int m,int n) { if(m==0) { return n+1; } if(m>0 && n==0) { return fby(m-1,1); } if(n>0 ) { return fby(m-1,fby(m,n-1)); } } int main(){ int n,m; cin>>m>>n; cout<<fby(m,n); return 0; } ``` $\color{red}问题是啥呢,这个if(n>0)$ $\color{red}需要return,不然如果一直为第3种情况的话就卡了(死循环)$ $\color{red}所以会RE$ # $\color{red}求关$
by 文锡 @ 2024-09-12 12:50:54


@[fanboyang](/user/1286089) ```cpp #include<bits/stdc++.h> using namespace std; int Ackermann(int m,int n) { if(m == 0) { return n + 1; } else if(m > 0 && n == 0) { return Ackermann(m - 1 , 1); } if(m > 0 && n > 0) { return Ackermann(m - 1, Ackermann(m , n - 1)); } } int main() { int m , n; cin >> m >> n; cout << Ackermann(m , n) << endl; return 0; }
by Jason101 @ 2024-09-17 18:43:28


求关QWQ(用的记忆化) ``` #include<cstdio> int f[1010][1010],n,m; int akm(int m,int n){ if(!m)return f[m][n]=n+1; else if(f[m][n])return f[m][n]; else if(m&&!n)return f[m][n]=akm(m-1,1); else return f[m][n]=akm(m-1,akm(m,n-1)); } int main(){ scanf("%d%d",&m,&n);akm(m,n); printf("%d",f[m][n]);return 0; } ```
by jebfghbbcdsl @ 2024-09-27 21:15:42


@[fanboyang](/user/1286089)
by jebfghbbcdsl @ 2024-09-27 21:39:52


hhhhhhhh
by _liujunming_ @ 2024-10-01 15:48:25


|