返回列表 發帖
  1. import java.util.Scanner;

  2. public class Ch01 {
  3.     static int f(int a)
  4.     {
  5.             if(a==0)
  6.                     return 0;
  7.             if(a==1)
  8.                     return 1;
  9.             else
  10.                     return f(a-2)+f(a-1);
  11.     }
  12.         public static void main(String[] args) {
  13.                 int n;
  14.                 Scanner s=new Scanner(System.in);
  15.                 System.out.print("請問要推算費氏樹列到第幾項? ");
  16.                 n=s.nextInt();
  17.                 for(int i=0; i<=n; i++)
  18.                 {
  19.                         System.out.print(f(i)+"  ");
  20.                 }
  21.         }
  22. }
複製代碼

TOP

返回列表