1.一只青蛙一次可以跳上 1 级台阶,也可以跳上2 级。求该青蛙跳上一个n 级的台阶总共有多少种跳法
public static int jumpFloor(int target){
if(target==1){
return 1;
}else if(target==2){
return 2;
}else{
return jumpFloor(target-1)+jumpFloor(target-2);
}
}
public static void main(String[] args) {
int n=4;
System.out.println(jumpFloor(n));
}
2变态跳台阶:一次可以跳1阶,2阶…n阶
public static int JumpFloorII(int target) {
if(target <= 2){
return target;
}else{
return JumpFloorII(target - 1) * 2;
}
//f(n)=f(n-1)+f(n-2)+...f(1)
//f(n-1)=f(n-2)+f(n-3)+...f(1)
//两式相减得f(n)=2*f(n-1)
}
public static void main(String[] args) {
int n=3;
System.out.println(JumpFloorII(n));
}