动态规划--爬楼梯
2023-04-12 22:37
views:
536
source:
bigseal
#include <stdio.h>
#include <vector>
//爬楼梯若n为100,则最后一步只能是从99阶开始或从98阶开始
//所以f(100)=f(99)+f(98)
using namespace std;
int cilmb_stairs(int n)
{
vector<int>dp(n + 1);
if (n <= 0) return n;
dp[1] = 1; dp[2] = 2;
for (int i = 3; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
int main()
{
int result = cilmb_stairs(6);
printf("%d", result);
}
Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.