爬楼梯

//爬楼梯
import java.util.Scanner;

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

        int m = sc.nextInt();
        int sum2 = method2(n, m);
        System.out.println(sum2);
    }

    /**若最大可跳跃2个台阶,裴波那契数列
     *n=1  一个台阶,一种跳法;
     *n=2  两个台阶,一个一个跳或者一次跳两个,两种跳法
     *n>=2 分两类:第一次跳一个台阶,剩下n-1个台阶,也就是说跳法数和n-1个台阶的相同;
     * 第一次跳两个台阶,剩下n-2个台阶,也就是说跳法数和n-2个台阶的相同
     * 所以总的跳法是前两种之和。
     * 用同样的思想可以推出一次最大跳跃m个台阶的总跳法
     * */
    public static int method(int n) {
        if (n <= 2) {
            return n;
        } else {
            return method(n - 1) + method(n - 2);
        }
    }
    //若最大可跳跃m个台阶
    public static int method2(int n, int m) {
        int jump = 0;
        if (n == 0) {
            return 1;
        }
        if (n >= m) {
            for (int i = 1; i <= m; i++) {
                jump += method2(n - i, m);
            }
        } else {
            jump += method2(n, n);
        }
        return jump;
    }
}
原文地址:https://www.cnblogs.com/21556guo/p/13471132.html