题目描述:
有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
代码:
java
package lanqiao;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++)
{
int m = sc.nextInt();
System.out.println(f(m - 1));
}
}
public static int f(int n)
{
if(n == 1) return 1;
if(n == 2) return 2;
int a = 1;
int b = 2;
int temp = 0;
for(int i = 3;i <= n;i ++)
{
temp = a + b;
a = b;
b = temp;
}
return temp;
}
}