斐波那契數列

斐波那契數列,輸出斐波那契數列的第n項(從0開始,第0項爲0)。

import java.lang.*;
public  class Main {
public static void main(String args[]) {
  Solution solution = new Solution();
       System.out.println(solution.Fibonacci(4));
    }
}
class Solution {
    public int Fibonacci(int n) {
        int F1 = 1;
        int F2 = 1;
        int F3=F1+F2;
        while (n>=4) {
            F1 = F2;
            F2 = F3;
         F3 = F2 + F1;
            n--;
      }
        if(n==0)
            return 0;
        else if(n==1 ||n==2)
            return 1;
        else
           return F3;

    }
    }
發佈了16 篇原創文章 · 獲贊 0 · 訪問量 717
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章