题目描述
楼梯有 N 阶,上楼可以一步上一阶,也可以一步上二阶。
编一个程序,计算共有多少种不同的走法。
输入格式
一个数字,楼梯数。
输出格式
输出走的方式总数。
输入输出样例
输入
4
输出
5
说明/提示
- 对于 60% 的数据,N≤50;
- 对于 100% 的数据 ,N≤5000。
由于部分数据较大,会超出int的范围,可以考虑使用java中的BigInteger来完美解决这个问题
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
BigInteger[] steps = new BigInteger[5001];
BigInteger one = new BigInteger("1");
BigInteger two = new BigInteger("2");
steps[1] = one;
steps[2] = two;
for (int i = 3; i <=5000 ; i++) {
steps[i] = steps[i-1].add(steps[i-2]);
}
System.out.println(steps[n]);
}
}