斐波那契数列

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
package com.hundsun.netease;
/**
* Created by zeewane on 2016/8/4/0004.
*/
public class FibonacciDemo {
public static void main(String[] args) {
System.out.println(Fibonacci(39));
}
public static int Fibonacci(int n) {
int preNum = 1;
int prePreNum = 0;
int result = 0;
if (n == 0)
return 0;
if (n == 1)
return 1;
for (int i = 2; i <= n; i++) {
result = preNum + prePreNum;
prePreNum = preNum;
preNum = result;
}
return result;
}
}

用循环的方法去解决,不要用递归。