台阶问题练习题 (简单的dp)

题目:

有n级台阶,一个人每次上一级或者两级,问有多少种走完n级台阶的方法。为了防止溢出,请将结果Mod 1000000007

给定一个正整数int n,请返回一个数,代表上楼的方式数。保证n小于等于100000。

测试样例:1
返回: 1
 
到达n台阶时,可以有两种方法到达n台阶,只需走一级就可以上去,或者只需走两级就可以上去,哦了
昨天晚上搞得dp几道经典题目,抽个时间把那几道在理解下。
 
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 int s[100010]={0,1,2};
 6 const int c=1000000007;
 7 int main()
 8 {
 9     int n;
10     for(int i=3;i<=100000;i++)
11         s[i]=(s[i-1]%c+s[i-2]%c)%c;
12     while(~scanf("%d",&n))
13     {
14         printf("%d
",s[n]);
15     }
16     return 0;
17 }
原文地址:https://www.cnblogs.com/WDKER/p/5486909.html