使用遞迴的 Java 中的斐波那契數列程式。


以下是必需的程式。

範例

即時演示

public class Tester {
   static int n1 = 0, n2 = 1, n3 = 0;
   static void fibbonacci(int count) {
      if (count > 0) {
         n3 = n1 + n2;
         n1 = n2;
         n2 = n3;
         System.out.print(" " + n3);
         fibbonacci(count - 1);
      }
   }
   public static void main(String args[]) {
      int count = 5;
      System.out.print(n1 + " " + n2);
      fibbonacci(count - 2);
   }
}

輸出

0 1 1 2 3

更新於: 05-03-2020

2K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.