问题:一只青蛙一次可以跳上 1 级台阶,也可以跳上2 级。求该青蛙跳上一个n 级的台阶总共有多少种跳法
思路:1层台阶 跳1次
2层台阶 跳2次
3层台阶 跳3=1+2次
4层台阶 跳5=2+3次
代码:
public static int step(int n){
if(n==1){
return 1;
}
else if(n==2){
return 2;
}
else{
return(step(n-1)+step(n-2));
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println(step(n));
}