跳台阶问题

题目:一个台阶总共有n级,如果一次可以跳1级,也可以跳2级。求总共有多少总跳法。

f(n)=f(n-1)+f(n-2),变成求费伯纳西数列

//跳台阶
#include<iostream>
using namespace std;
int step_two(int num_of_stair){
    int before=1;
    int after=2;
    int total;
    if(num_of_stair==1) return 1;
    else if(num_of_stair==2) return 2;
    else{
            for(int i=3;i<=num_of_stair;i++){
                total=before+after;
                before=after;
                after=total;
            }
            return total;
    }
}
int main(void){
    int num;
    cin>>num;
    cout<<step_two(num)<<endl;
    system("pause");
    return 0;
}

posted @   akawhy  阅读(446)  评论(0编辑  收藏  举报
编辑推荐:
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
阅读排行:
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· 大模型工具KTransformer的安装
· [计算机/硬件/GPU] 显卡
点击右上角即可分享
微信分享提示