6 solutions
-
2
用 dp[i] 表示走到第 i 个台阶数的方案数
第 i 级台阶可以由第 i-1 和第 i-2 级台阶上来
所以递推公式 dp[i]=dp[i-1]+dp[i-2]
递推初始值为 dp[1]=1,dp[2]=2
#include<bits/stdc++.h> using namespace std; #define ioio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define endl "\n" #define debug(x) cout<<#x<<":"<<x<<endl; #define L(k) k<<1 #define R(k) k<<1|1 #define P pair #define P1 first #define P2 second #define u_map unordered_map #define p_queue priority_queue typedef long ll; const double eps = 1e-6; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const int INF2 = (1 << 31); const int N = 1e6 + 7; int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; /*-------------------------------------------------*/ int n; ll dp[N]; int main() { ioio dp[1]=1,dp[2]=2; for(int i=3;i<=75;i++) dp[i]=dp[i-1]+dp[i-2]; cin>>n; cout<<dp[n]<<endl; return 0; }
-
1
这题和Fibonacci数列比较相似,本质上是一个简单的动态规划。 递推公式为:dp[i] = dp[i-1] +dp[i-2] 注意dp数组的初始化,dp[1]=1,dp[2]=2。
#include<bits/stdc++.h> using namespace std; typedef long long LL; LL dp[100]; int main(){ // dp[n] = dp[n-1] + dp[n-2] // dp[0] = ? dp[1] = 1 dp[2] = 2 int n; cin>>n; dp[1]=1;dp[2]=2; for(int i=3;i<=n;i++){ dp[i] = dp[i-1]+dp[i-2]; } cout<<dp[n]; return 0; }
- 1
Information
- ID
- 49
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 7
- Tags
- # Submissions
- 565
- Accepted
- 140
- Uploaded By