forked from echoTheLiar/JavaCodeAcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
39 lines (31 loc) · 939 Bytes
/
Fibonacci.java
File metadata and controls
39 lines (31 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package basic;
import java.util.Arrays;
/**
* 斐波那契数列
* @author tanghuan
* @date 2024年11月29日
*/
public class Fibonacci {
public static void main(String[] args) {
long start = System.currentTimeMillis();
long fibonacci = getFibonacci(100);
long end = System.currentTimeMillis();
System.out.println("耗时:"+(end-start)/1000+"结果:"+fibonacci);
}
public static long getFibonacci(int n) {
if (n < 1) {
return -1;
}
if (n == 1 || n == 2) {
return 1;
}
long[] arr = new long[n];
arr[0] = arr[1] = 1; //第一个和第二个数据特殊处理
for (int i = 2; i < n; i++) {
arr[i] = arr[i -2] + arr[i - 1];
}
//可以得到整个的数列数据 仅n>2
System.out.println("数组内容:" + Arrays.toString(arr));
return arr[n - 1];
}
}