Java 实现斐波那契数列

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
public class Fibonacci {
public static void main(String[] args) {
System.out.println(fibonacci1(9));
System.out.println(fibonacci2(9));
}
// 递归实现
public static int fibonacci1(int n) {
if (n <= 2) {
return 1;
} else {
return fibonacci1(n-1) + fibonacci1(n - 2);
}
}

// 递推实现
public static int fibonacci2(int n) {
if (n <= 2) {
return 1;
} else {
int n1 = 1, n2 = 1, sn = 0;
for (int i = 0; i < n-2; i++) {
sn = n1 + n2;
n1 = n2;
n2 = sn;
}
return sn;
}
}
}