题目:
斐波那契数列,输入n,求斐波那契数列的第n项值
F0=0,F1=1,F2 = 1,... , Fn = Fn-1 + Fn-2
思路:
循环
代码实现:
public class Seventh { public static long getFibonacci(int n){ if(n <= 0){ return 0; } if(n == 1 & n == 2){ return 1; } int prePre = 1; int pre = 1; int current = 2; for(int i = 3; i <= n; i ++){ current = prePre + pre; prePre = pre; pre = current; } return current; } }