题解 | #爬楼梯#

爬楼梯

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

import java.util.Scanner;
import java.math.BigInteger;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int stepCount = scanner.nextInt();
            System.out.println(climeStep(new BigInteger(String.valueOf(stepCount))));
        }
    }

    public static BigInteger climeStep(BigInteger stepCount) {
        if (stepCount.intValue() == 1) {
            return new BigInteger("1");
        }
        if (stepCount.intValue() == 2) {
            return new BigInteger("2");
        }

        BigInteger step[] = new BigInteger[stepCount.intValue()];
        step[0] = new BigInteger("1");
        step[1] = new BigInteger("2");

        for (int i = 2; i < stepCount.intValue(); i++) {
            step[i] = step[i - 1].add(step[i - 2]);
        }

        return step[stepCount.intValue() - 1];
    }
}

全部评论

相关推荐

自来熟的放鸽子能手面...:这个不一定,找hr跟进一下
点赞 评论 收藏
分享
09-01 21:40
已编辑
同济大学 Java
点赞 评论 收藏
分享
评论
1
3
分享

创作者周榜

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