题目
思路
采用动态规划的思路
经过分析可以发现,每次只能跨一级或者两级台阶,所以动态方程为:
也就是说,第n级台阶的结果等于第n-1级台阶的结果加上第n-2级台阶的结果
再来考虑一下初始条件,很容易的可以算出,第0级=1,第1级=1,根据这两个初始条件便可以算出第2级...第n级的结果了
代码如下
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int n = sc.nextInt();
System.out.println(climbStairs(n));
}
}
public static int climbStairs(int n){
int a = 0;