本质是斐波那契数列的变形
public static int flogStep1(int n) {
//递归写法
if (n == 1 || n == 2) {
return n;
} else {
return flogStep1(n - 1) + flogStep1(n - 2);
}
}
public static int flogStep2(int n) {
//循环写法
//实质就是求斐波那契数列
if (n == 1 || n == 2) {
return n;
}
int f1 = 1;
int f2 = 2;
int f3 = 0;
for (int i = 3; i <= n; i++) {
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
return f3;
}
public static void main(String[] args) {
Scanner step = new Scanner(System.in);
int n = step.nextInt();
System.out.println(flogStep1(n));
System.out.println(flogStep2(n));
}