时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:1598
解决:641
- 题目描述:
-
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
- 输入:
-
输入可能包含多个测试样例,对于每个测试案例,
输入包括一个整数n(1<=n<=70)。
- 输出:
-
对应每个测试案例,
输出该青蛙跳上一个n级的台阶总共有多少种跳法。
- 样例输入:
-
5
- 样例输出:
-
8
递归无法通过,所以还是用DP
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class S9_2 {
public static void main(String[] args) throws FileNotFoundException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream("S9_2.in"));
System.setIn(in);
Scanner cin = new Scanner(System.in);
while (cin.hasNextInt()) {
int n = cin.nextInt();
System.out.println(steps2(n));
}
}
public static long steps(int n){
if(n == 1){
return 1;
}
if(n == 2){
return 2;
}
return steps(n-1) + steps(n-2);
}
public static long steps2(int n){
long[] dp = new long[n+10];
dp[0] = 1;
dp[1] = 2;
for(int i=2; i<=n; i++){
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n-1];
}
}
2057



被折叠的 条评论
为什么被折叠?



