HDU 2041 DP

URL:https://vjudge.net/problem/HDU-2041

简单DP,因为每次只能走1或者2阶,所以当走到第i阶的时候(i>=4),那么它的前一种状态只可能是i-1和i-2,所以状态方程为:

DP[2] = 1;

DP[3] = 2;

DP[i] = DP[i-1] + DP[i-2];(i>3)

AC code :

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <bits/stdc++.h>
 4 using namespace std;
 5 int main()
 6 {
 7     int n;
 8     int a;
 9     int DP[41];
10     DP[2] = 1;
11     DP[3] = 2;
12     for(int i = 4;i < 41;i++)
13     {
14         DP[i] = DP[i-1] + DP[i-2];
15     }
16     cin >> n;
17     for(int i = 1;i <= n;i++)
18     {
19         cin>>a;
20         cout<<DP[a]<<endl;
21     }
22     return 0;
23 }

 

posted on 2019-07-14 17:02  Algorithm_Ethusiast  阅读(199)  评论(0编辑  收藏  举报

导航