输出爬到楼梯顶部的方法总数。5-->8 3-->3 1-->1 2 -->2
package com.para.java.Algorithm;
/**
* @author :yinmy
* @date :Created on 2020/6/22 9:20
*/
import java.util.Scanner;
/**
* 假设你现在正在爬楼梯,每次只能爬1级或者2级,那么你有多少种方法爬到楼梯的顶部
* 第一行输入一个楼梯的级数n(1<=n<=50)
* 输出爬到楼梯顶部的方法总数 5-->8 3-->3 1-->1 2 -->2
* 分析:最后一次爬只有两种情况,不是1级 就是2级,即Method(n) = Method(n-1)+Method(n-2)
*/
public class ClimbStairs {
public int Method(int n){
if(n == 1){
return 1;
}
if(n == 2){
return 2;
}
return Method(n-1)+Method(n-2);
}
public static void main(String[] args) {
ClimbStairs climbStairs = new ClimbStairs();
Scanner input = new Scanner(System.in);
String nextLine = input.nextLine();
int n = Integer.valueOf(nextLine);
int number = climbStairs.Method(n);
System.out.println(number);
}
}
