题目 1257: 超级楼梯

题目描述:

有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?

代码:

java 复制代码
package lanqiao;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++)
        {
            int m = sc.nextInt();
            System.out.println(f(m - 1));
        }
    }

    public static int f(int n)
    {
        if(n == 1) return 1;
        if(n == 2) return 2;

        int a = 1;
        int b = 2;
        int temp = 0;
        for(int i = 3;i <= n;i ++)
        {
            temp = a + b;
            a = b;
            b = temp;
        }
        return temp;
    }
}
相关推荐
ZhouDevin5 分钟前
算法论文/数据集5——AlignPrune(CVPR2026)基于样本损失时间序列精确识别噪声样本
算法
深入云栈21 分钟前
CompleteFuture VS CompletableFuture:Netty 为何自研 Future
java·架构
OPEN-F21 分钟前
C++综合实战:面向对象图书管理系统
开发语言·c++
ttwuai42 分钟前
Go 后台清空操作日志失败,权限和无 WHERE 删除怎么排查?
开发语言·后端·golang
choumou_M43 分钟前
SpringBoot_7:用户资源的上传与修改
java·spring boot·后端
十铭忘1 小时前
HMM(隐马尔可夫模型)的理解7——Baum–Welch 算法
人工智能·算法
西西弗Sisyphus1 小时前
Qt 在无边框窗口上做一套换肤系统
开发语言·数据库·qt
西西弗Sisyphus1 小时前
Qt 启动 UsageStatistic 插件报错
开发语言·qt
NeoGressAI外贸数字化1 小时前
外贸独立站零询盘排查:从 Google Search Console 到 PageSpeed 的技术实操
开发语言·c++
无小道1 小时前
C/C++——异步编程小记
开发语言·c++·c++11