题目:java实现一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
package cn.it.Solution;
import java.util.Scanner;
public class SolutionDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("请输入楼梯阶数:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int target = RectCover(num);
System.out.println("一共"+target+"种跳发");
}
private static int RectCover(int num) {
if (num <= 2) {
return num;
}
int f0 = 1;
int f1 = 2;
int f2 = 0;
for (int i = 3; i <= num; i++) {
f2 = f1 + f0;
f0 = f1;
f1 = f2;
}
return f2;
}
}