题解 | 跳台阶
跳台阶
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); // } } }