- import java.util.Scanner;
- public class test {
- public static void main(String[] args) {
- int temp = 0;
- int a = 0, b = 1;
- Scanner scanner = new Scanner(System.in);
- int num = scanner.nextInt();
- if (num == 0) {
- System.out.println(0);
- }
- for (int i = 2; i < num ; i++) {
- temp = a;
- a = b;
- b += temp;
- }
- System.out.println(b);
- }
- }
复制代码 递归的方法实在太消耗系统资源了。时间开销和空间开销都好大。最好的解决办法是不用递归。 |