返回列表 發帖

遞迴函式 (二) - 費氏數列

本帖最後由 tonyh 於 2019-1-25 13:26 編輯

費氏數列 - 維基百科

費氏數列規則如下:
第n項 = 第 n-1 項  + 第 n-2 項

即整個費式數列為:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377...

試完成一程式,能推算費氏數列至指定項次:

  1. import java.util.Scanner;

  2. public class Ch50 {       
  3.    
  4.         static int fai(int n)
  5.         {
  6.                 if(n<2)
  7.                         return n;
  8.                 else
  9.                         return fai(n-2)+fai(n-1);
  10.         }
  11.         /*
  12.             
  13.             值:  0 1 1 2 3 5 8
  14.             項:  0 1 2 3 4 5 6
  15.             
  16.            fai(5)
  17.            =fai(3)+fai(4)
  18.            =fai(1)+fai(2)+fai(2)+fai(3)
  19.            =1+fai(0)+fai(1)+fai(0)+fai(1)+fai(1)+fai(2)
  20.            =1+0+1+0+1+1+fai(0)+fai(1)   
  21.            =1+0+1+0+1+1+0+1     
  22.            =5
  23.           
  24.         */
  25.         public static void main(String[] args)
  26.         {
  27.                 int n;
  28.                 Scanner s=new Scanner(System.in);
  29.                 System.out.print("請問要推算費氏數列到第幾項次? ");
  30.                 n=s.nextInt();
  31.                 for(int i=0; i<=n; i++)
  32.                 {
  33.                         System.out.print(fai(i)+" ");
  34.                 }
  35.         }
  36. }
複製代碼

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

TOP

本帖最後由 楊貳鈞 於 2019-1-25 13:40 編輯
  1. import java.util.Scanner;
  2. public class Ch03
  3. {
  4.         static int fai(int n)
  5.           {
  6.                   if(n<2)
  7.                           return n;
  8.                   else
  9.                           return fai(n-2)+fai(n-1);
  10.            }
  11.         public static void main(String[] args)
  12.           {
  13.                         Scanner s=new Scanner(System.in);
  14.                         int n;
  15.                         System.out.print("請問要推算費氏數列到第幾項次? ");
  16.                         n=s.nextInt();
  17.                 for(int i=0; i<=n; i++)
  18.              {
  19.                  System.out.print(fai(i)+" ");
  20.              }
  21.        }
  22. }
複製代碼

TOP

本帖最後由 楊于暄 於 2019-1-25 13:36 編輯
  1. import java.util.Scanner;
  2. public class Ch33
  3. {
  4.         static int fai(int n)
  5.         {
  6.                 if(n<2)
  7.                         return n;
  8.                 else
  9.                         return fai(n-2)+fai(n-1);
  10.         }
  11.         public static void main(String[] args)
  12.         {
  13.                 Scanner s=new Scanner(System.in);
  14.                 int n;
  15.                 System.out.print("請問要推算費是數列到第幾項? ");
  16.                 n=s.nextInt();
  17.                 for(int i=0; i<=n; i++)
  18.                         System.out.print(fai(i)+" ");
  19.         }
  20. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch16 {
  3.         static int fai(int n)
  4.         {
  5.                 if(n<2)
  6.                         return n;
  7.                 else
  8.                         return fai(n-2)+fai(n-1);
  9.         }

  10.         public static void main(String[] args) {
  11.                  int n;
  12.          Scanner s=new Scanner(System.in);
  13.          System.out.print("請問要推算費氏數列到第幾項次? ");
  14.          n=s.nextInt();
  15.          for(int i=0; i<=n; i++)
  16.          {
  17.                  System.out.print(fai(i)+" ");
  18.          }

  19.         }

  20. }
複製代碼

TOP

  1. import java.util.Scanner;

  2. public class Ch12
  3. {
  4.         static int count(int a)
  5.         {               
  6.                         if(a==1)
  7.                                 return 1;
  8.                         if(a==0)
  9.                                 return 0;
  10.                         else
  11.                                 return count(a-1)+count(a-2);       
  12.         }
  13.         public static void main(String[] args)
  14.         {
  15.                 int a;
  16.                 Scanner s=new Scanner(System.in);
  17.                 System.out.print("請輸入你要計算到第幾項次: ");
  18.                 a=s.nextInt();       
  19.                 for(int i=0;i<a;i++)
  20.                 {
  21.                         System.out.print(" "+count(i));
  22.                 }
  23.         }
  24. }
複製代碼

TOP

  1. import java.util.Scanner;

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

TOP

返回列表