题解 | #爬楼梯#
爬楼梯
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];
}
}
查看25道真题和解析