intack(int m, int n){ if (m == 0){ return n += 1; } if (m > 0 && n == 0){ returnack(m - 1, 1); } if (m > 0 && n > 0){ returnack((m - 1), ack(m, n - 1)); } }
intmain(){ cin >> x >> y; ans = ack(x, y); cout << ans; }
运行结果:
解决方法:(超水(nan))
拿出草稿纸,把阿克曼函数m值从0到3的展开算了一遍,如下:
\(ack(0, n) = n + 1\)
\(ack(1, n) = n + 2\)
\(ack(2, n) = 2n + 3\)
\(ack(3, n) = 2^{y + 3} - 3\)
最终代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include"iostream" #include"cmath"
usingnamespace std;
int x, y;
intmain(){ cin >> x >> y; if (x == 0){ cout << y + 1; }elseif (x == 1){ cout << y + 2; }elseif (x == 2){ cout << 2 * y + 3; }elseif (x == 3){ cout << pow(2, y + 3) - 3; } }