爬楼梯

论坛 期权论坛 脚本     
已经匿名di用户   2022-4-13 16:43   1889   0

有一段楼梯台阶共有15级,小明一步最多只能跨3级,请问小明登上这段台阶有多少种不同走法?

#include<iostream>
#include<string.h>
using namespace std;
int f(int n) {
 int a=0;
 if(n==1) return 1;
 if(n==2) return 2;
 if(n==3) return 4;
 return a=f(n-1)+f(n-2)+f(n-3);
 return a;
}
int main() {
 int n;
 cin>>n;
 cout<<f(n);
 return 0;
}

最终输出结果:5768.

裴波那契思想,1个台阶1种走法,2个台阶有2种走法(2,1+1),3个台阶4种走法(3,1+2,2+1,1+1+1),所以n个台阶有f(n-1)+f(n-2)+f(n-3).

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP