这是数据结构老师布置的第四道题:

题目要求如下:

走台阶

很典型的动态规划问题,可以用递归求解,代码如下:

#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;
}

运行结果如下:

走台阶

相关文章:

  • 2021-08-30
  • 2022-12-23
  • 2022-01-14
  • 2021-11-12
  • 2021-09-13
猜你喜欢
  • 2021-09-18
  • 2021-09-17
  • 2021-11-10
  • 2022-12-23
  • 2021-08-15
相关资源
相似解决方案