2、爬楼梯问题

问题描述
一段楼梯共n级台阶,每次只能走一级或两级,问共有多少种走法?列出n=10时的所有走法。

【碎碎念】
这是紧接着我找了个题来做,事实上,这是老师课上曾提到的一个题。突然发现很简单,因为我很容易想到了原理。
嗯,或许这两道递归题实际上就是斐波那契数列的变形应用。

#include <iostream>
#include <stdlib.h>
using namespace std;
int fun(int n)
{

	if (n == 1)
		return 1;
	if (n == 2)
		return 2;
	if( n >= 3)
	return fun(n - 2) + fun(n - 1);
}
int main()
{
	int n;
	cin >> n;
	cout << "方案数:" << fun(n) << endl;
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/jy_z11121/article/details/83475729