首页 > 代码库 > FibonacciSequence

FibonacciSequence

import java.util.Scanner;

public class Fibonacci {

public static void main(String[] args) {

int n;
for (;;) {// 无限循环
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
while (n < 1 || n > 1000000) {
n = sc.nextInt();
}
break;
}

int[] array = new int[n];
for (int i = 0; i < array.length; i++) {
if (i == 0) {// 第1项是1,
array[i] = 1;
} else if (i == 1) {// 第2项是1
array[i] = 1;
} else {// 从第3项开始,每一项都等于前两项的和
array[i] = (array[i - 2] + array[i - 1]) % 10007;
}
}
System.out.println(array[n - 1]);
}

}

FibonacciSequence