ackerman函数的定义如下:
n+1 m=0;
ackerman(m,n)= ackerman(m-1,1) n=0,m>0;
ackerman(m-1,ackerman(m,n-1)) n>0,m>0;
我们定义两个数组:val和ind,并设 val【m】=ackerman(m,ind【m】);
则可以设计如下算法:
LongDataType Ackerman(int m, int n)
{
ind[0] = 0;
Val[0] = 0;
ind[m] = -1;
for (int i = 1; ind[m] < n; i++)
{
if (ind[i – 1] == 1)
{
val[i] = val[i – 1];
ind[i] = 0;
}
else if (ind[i – 1] == val[i])
{
val[i] = val[i – 1];
ind[i]++;
}
else
{
i = 0;
ind[0]++;
val[0]++;
}
}
return val[m];
}