超級樓梯
題目描述:
有一樓梯共M級,剛開始時你在第一級,若每次只能跨上一級或二級,要走上第M級,共有多少種走法?
Input :
輸入數據首先包含一個整數N,表示測試實例的個數,然後是N行數據,每行包含一個整數M(1<=M<=40),表示樓梯的級數。
Output:
對於每個測試實例,請輸出不同走法的數量
Sample Input:
Sample Output:
思路分析:
這是最容易理解的DP入門,這道題只要找出狀態就行了,那麼這裏舉個例子
方便大家理解,如果是給出是第4級臺階:
第一種: 3 -> 4
第二種: 2 -> 4
以此類推將4換成3或2繼續遞推,那麼就有了一個公式:
F(n)=F(n-1)+F(n—2).
代碼:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
using namespace std;
void LemonDP(int c)
{
int dp[c+1];
dp[1]=0;
dp[2]=1;
dp[3]=2;
for(int i=4;i<=c;i++)
{
dp[i]=dp[i-1]+dp[i-2];
}
cout << dp[c] << endl;
}
int main()
{
int b,c;
cin >> b;
while(b--)
{
scanf("%d",&c);
LemonDP(c);
}
}