题解 | 跳台阶

跳台阶

https://www.nowcoder.com/practice/bfb2a2b3cdbd4bd6bba0d4dca69aa3f0

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    public static int getStep(int count) {
        if (count == 1) return 1;
        if (count == 2) return 2;

        return getStep(count - 1) + getStep(count - 2);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = in.nextInt();
        int mt1 = 1;
        int mt2 = 2;

        if (count == 1 || count == 2) {
            System.out.println(count);
            return ;
        }


        for (int i = 3; i <= count; i++) {
            int temp = mt2;

            mt2 = mt1 + mt2;
            mt1 = temp;
        }

        System.out.println(mt2);
        // 注意 hasNext 和 hasNextLine 的区别
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
    }
}

全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
屌丝逆袭咸鱼计划:心态摆好,man,晚点找早点找到最后都是为了提升自己好进正职,努力提升自己才是最关键的😤难道说现在找不到找的太晚了就炸了可以鸡鸡了吗😤早实习晚实习不都是为了以后多积累,大四学长有的秋招进的也不妨碍有的春招进,人生就这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务