ZJANS

d212. 東東爬階梯

Medium Last Update: 2026/01/30
DP

給一數字 n,代表階梯有 n 階
每一次可以選擇 走一階 或是 走兩階
求走完 n 階的方法數


解法一、DP

✅ 完整代碼

評分結果(參考) : AC (1ms, 392KB)

#include<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    vector<int> dp(10000);
    dp[0] = 1;
    dp[1] = 2;
    for(int i=2; i<10000; i++){
        dp[i] = (dp[i-1]+dp[i-2]);
    }
    
    int n;
    while(cin >> n){
        cout << dp[n-1] << "\n";
    }
    
    return 0;
}