变态跳台阶

同普通跳台阶一样,使用递归就可以解决,不过普通版因为仅有跳1阶和2阶的选择,所以返回F(n-1)+F(n-2),而变态版返回F(n-1)+F(n-2)+...+F(1)+1。此处加1是因为变态版除了可以跳(n-1)、(n-2)、...、2、1的和之外,还额外多一种跳n阶的情况。

public int jumpFloorGreed(int n)
    {
        int res = 0;
        if(n==0)
            return 0;
        else if(n==1)
            return 1;
        else
        {
            for(int i=1;i<n;i++)
            {
                res += jumpFloorGreed(i);
            }
            return res+1;
        }    
    }

 

posted @ 2019-10-22 11:43  江晓白  阅读(83)  评论(0编辑  收藏  举报