//带备忘的二维辅助空间计算Ackermann
int Ackermann(m, n) {
1. if A[m,n] >= 0
2. then return A[m, n];
3. else if m = 0 and n > 0
4. then A[m,n] <- n + 1;
5. else if n = 0 and m > 0
6. then A[m,n] <- Ackermann(m-1, 1);
7. else if n > 0 and m > 0
8. then A[m,n] <- Ackermann(m-1, Ackermann(m, n-1));
9. return A[m, n];
}
还有一个方法只需要用到O(m)的空间复杂度
int computeACK(int m, int n) {
if(m<0||n<0) return -1;
if(m==0) return n+1;
int* val = new int[n+1];
int* ind = new int[n+1];
for(int i = 0; i <= n; i++) val[i] = ind[i] = -1;
val[0] = 1;
ind[0] = 0;
while(ind[m]<n) {
val[0]++;
ind[0]++;