<Prove safety offer> Question 7

topic:

Fibonacci number, type n, the first n items seeking Fibonacci value of Number of

F 0 = 0, F 1 = 1, F 2  = 1, ..., F = the Fn N-1  + F N-2

Ideas:

cycle

 

Code:

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;
    }
}

 

Guess you like

Origin www.cnblogs.com/HarSong13/p/11325096.html