Ackerman函数,可能跟网上的定义有点不一样,王晓东算法书中的版本。
最后一个Ackerman(4,3),在x86下编译可以运行得到正确结果,在x64下编译却运行错误,没有更多提示。用的是vs2019。
#include <iostream>
using namespace std;
int Ackerman(int n, int m)
{
if (n == 1 && m == 0)
return 2;
if (n == 0)
return 1;
if (m == 0)
return n + 2;
return Ackerman(Ackerman(n - 1, m), m - 1);
}
int main()
{
cout << "m = 0 : " << endl;
cout << "Ackerman(1,0) = " << Ackerman(1, 0) << endl;
cout << "Ackerman(2,0) = " << Ackerman(2, 0) << endl;
cout << "Ackerman(3,0) = " << Ackerman(3, 0) << endl;
cout << "Ackerman(4,0) = " << Ackerman(4, 0) << endl;
cout << "m = 1 : " << endl;
cout << "Ackerman(1,1) = " << Ackerman(1, 1) << endl;
cout << "Ackerman(2,1) = " << Ackerman(2, 1) << endl;
cout << "Ackerman(3,1) = " << Ackerman(3, 1) << endl;
cout << "Ackerman(4,1) = " << Ackerman(4, 1) << endl;
cout << "m = 2 : " << endl;
cout << "Ackerman(1,2) = " << Ackerman(1, 2) << endl;
cout << "Ackerman(2,2) = " << Ackerman(2, 2) << endl;
cout << "Ackerman(3,2) = " << Ackerman(3, 2) << endl;
cout << "Ackerman(4,2) = " << Ackerman(4, 2) << endl;
cout << "m = 3 : " << endl;
cout << "Ackerman(1,3) = " << Ackerman(1, 3) << endl;
cout << "Ackerman(2,3) = " << Ackerman(2, 3) << endl;
cout << "Ackerman(3,3) = " << Ackerman(3, 3) << endl;
cout << "Ackerman(4,3) = " << Ackerman(4, 3) << endl;
return 0;
}
--
FROM 60.176.118.*