• 跳台阶


    跳台阶

    题目:一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法

    package sort;

    import java.util.Scanner;

    /**
     * @author WangXiaoeZhe
     * @Date: Created in 2019/11/22 12:43
     * @description:
     */

    public class TaiJieSort {
        public static void main(String[] args{
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            System.out.println(f(n));
        }

        /**
         * 递归方式
         */

        private static int jumpFloor(int n{
            if (n <= 1) {
                return 0;
            }
            if (n <= 2) {
                return n;
            }
            return jumpFloor(n - 1) + jumpFloor(n - 2);
        }

        /**
         * 动态规划
         */

        public static int f(int n{
            if (n <= 0) {
                return 0;
            }
            int f = 1;
            int g = 1;
            while (n-- > 0) {
                g = g + f;
                f = g - f;
            }
            return f;
        }

        /**
         * 变态跳台阶问题(可以跳任何阶)
         */

        public static int F(int n{
            if (n < 0) {
                return 0;
            }
            if (n == 1) {
                return 1;
            }
            int f = 1;
            for (int i = 0; i <= n; i++) {
                f = 2 * f;
            }
            return f;
        }

    }
  • 相关阅读:
    Let the Balloon Rise
    Andy's First Dictionary
    myschool 1204
    vector容器
    766A Mahmoud and Longest Uncommon Subsequence
    python全栈开发-logging模块(日记专用)
    python全栈开发- day14列表推导式、生成器表达式、模块基础
    python全栈开发-Day13 内置函数
    python全栈开发-Day12 三元表达式、函数递归、匿名函数
    python全栈开发-Day11 迭代器、生成器、面向过程编程
  • 原文地址:https://www.cnblogs.com/wuhen8866/p/11910944.html
Copyright © 2020-2023  润新知