跳台阶

作者: lvlvforever | 来源:发表于2018-07-16 22:32 被阅读3次

    一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

    直接考虑n阶时的情况。第n阶台阶可以从第n-1台阶跳1级到达,也可以从n-2阶跳2级到达。可以得到公式
    f(n) = f(n-1) + f(n-2) 这其实就是斐波那契数列的表达式。

     public int JumpFloor(int target) {
            if (target == 1 || target == 2) {
                return target;
            }
            int first = 1;
            int second = 2;
            int third = 0;
            for (int i = 3; i <= target ; i++) {
                third = first + second;
                first = second;
                second = third;
            }
            return third;
        }
    

    类似的题目

    我们可以用21的小矩形横着或者竖着去覆盖更大的矩形。请问用n个21的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

    相关文章

      网友评论

        本文标题:跳台阶

        本文链接:https://www.haomeiwen.com/subject/uteipftx.html