一个求ackerman函数的非递归算法

    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];
    }

    原文作者:递归算法
    原文地址: https://blog.csdn.net/chc_chang_chc/article/details/1844812
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞