走台阶
这是数据结构老师布置的第四道题:
题目要求如下:
很典型的动态规划问题,可以用递归求解,代码如下:
#include"pch.h"
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int climbStairs(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
if (n == 2)
return 2;
if (n == 3)
return 4;
return climbStairs(n - 1) + climbStairs(n - 2)+climbStairs(n-3);
}
int main()
{
int n;
cout << "Enter the quantity of the stairs: ";
cin >> n;
cout << "Total methods: " << climbStairs(n) << endl;
}
运行结果如下: