Warning: mysqli_query(): (HY000/1021): Disk full (/tmp/#sql_51b_2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device") in /opt/lampp/htdocs/wordpress/wp-includes/wp-db.php on line 2033
Fibonacci数列()-其他 – 知识波

# Fibonacci数列()-其他

## Fibonacci数列()

Fibonacci数列的递推公式为：Fn=Fn-1+Fn-2，其中F1=F2=1。

`````` 1 import java.util.*;
2
3 public class Main {
4     public static void main(String[] args) {
5         Scanner scanner = new Scanner(System.in);
6         int n = scanner.nextInt();
7         long[] arr = new long[n+1];
8         arr[0] = 0;
9         arr[1] = 1;
10         for (int i = 2; i < n+1; i++) {
11
12             if (arr[i-1]>10007) {
13                 arr[i-1] = arr[i-1]%10007;
14             }
15             if (arr[i-2]>10007) {
16                 arr[i-2] = arr[i-2]%10007;
17             }
18             arr[i] = arr[i-1]+arr[i-2];
19         }
20         long result = (long) (arr[n]%10007);
21
22         System.out.println(result);
23     }
24 }``````

————————

Fibonacci数列的递推公式为：Fn=Fn-1+Fn-2，其中F1=F2=1。

`````` 1 import java.util.*;
2
3 public class Main {
4     public static void main(String[] args) {
5         Scanner scanner = new Scanner(System.in);
6         int n = scanner.nextInt();
7         long[] arr = new long[n+1];
8         arr[0] = 0;
9         arr[1] = 1;
10         for (int i = 2; i < n+1; i++) {
11
12             if (arr[i-1]>10007) {
13                 arr[i-1] = arr[i-1]%10007;
14             }
15             if (arr[i-2]>10007) {
16                 arr[i-2] = arr[i-2]%10007;
17             }
18             arr[i] = arr[i-1]+arr[i-2];
19         }
20         long result = (long) (arr[n]%10007);
21
22         System.out.println(result);
23     }
24 }``````