返回列表 發帖

費氏數列的兩種解法

本帖最後由 tonyh 於 2023-5-6 20:16 編輯

1. 遞迴
2. 動態規劃





Java
  1. // 0 1 1 2 3 5 8 ...
  2. public class Ch01 {

  3.         Ch01()
  4.         {
  5.                 System.out.println("費氏數列第12項: "+f(12));
  6.                 System.out.println("費氏數列第23項: "+f(23));
  7.                 System.out.println("費氏數列第37項: "+f(37));
  8.                 System.out.println("費氏數列第42項: "+f(42));
  9.         }

  10.         int f(int n)
  11.         {
  12.                 if(n<2)
  13.                         return n;
  14.                 else
  15.                         return f(n-2)+f(n-1);
  16.         }

  17.         public static void main(String[] args) {
  18.                 long start=System.currentTimeMillis();
  19.                 new Ch01();
  20.                 long end=System.currentTimeMillis();
  21.                 System.out.println("花費: "+(end-start)+" 毫秒");
  22.         }

  23. }
  24. /*
  25.     f(5)
  26.     =f(3)+f(4)
  27.     =f(1)+f(2)+f(2)+f(3)
  28.     =1+f(0)+f(1)+f(0)+f(1)+f(1)+f(2)
  29.     =1+0+1+0+1+1+f(0)+f(1)
  30.     =1+1+1+1+1=5
  31. */
複製代碼
  1. // 0 1 1 2 3 5 8 ...
  2. public class Ch02 {

  3.         Ch02()
  4.         {
  5.                 long data[]=new long[90];
  6.                 data[0]=0;
  7.                 data[1]=1;
  8.                 for(int i=2; i<90; i++)
  9.                         data[i]=data[i-2]+data[i-1];
  10.                 System.out.println("費氏數列第12項: "+data[12]);
  11.                 System.out.println("費氏數列第23項: "+data[23]);
  12.                 System.out.println("費氏數列第37項: "+data[37]);
  13.                 System.out.println("費氏數列第42項: "+data[42]);
  14.                 System.out.println("費氏數列第59項: "+data[59]);
  15.                 System.out.println("費氏數列第89項: "+data[89]);
  16.         }

  17.         public static void main(String[] args) {

  18.                 long start=System.currentTimeMillis();
  19.                 new Ch02();
  20.                 long end=System.currentTimeMillis();
  21.                 System.out.println("花費: "+(end-start)+" 毫秒");
  22.         }

  23. }
複製代碼
C++
  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int f(int n)
  4. {
  5.     if(n<2)
  6.         return n;
  7.     else
  8.         return f(n-2)+f(n-1);
  9. }

  10. int main()
  11. {
  12.     cout<<"費氏數列第12項: "<<f(12)<<endl;
  13.     cout<<"費氏數列第23項: "<<f(23)<<endl;
  14.     cout<<"費氏數列第37項: "<<f(37)<<endl;
  15.     cout<<"費氏數列第42項: "<<f(42)<<endl;
  16.     cout<<"花費: "<<clock()<<" 毫秒"<<endl;
  17.     return 0;
  18. }
複製代碼
  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     long long data[90];
  6.     data[0]=0;
  7.     data[1]=1;
  8.     for(int i=2; i<90; i++)
  9.         data[i]=data[i-2]+data[i-1];
  10.     cout<<"費氏數列第12項: "<<data[12]<<endl;
  11.     cout<<"費氏數列第23項: "<<data[23]<<endl;
  12.     cout<<"費氏數列第37項: "<<data[37]<<endl;
  13.     cout<<"費氏數列第42項: "<<data[42]<<endl;
  14.     cout<<"費氏數列第59項: "<<data[59]<<endl;
  15.     cout<<"費氏數列第89項: "<<data[89]<<endl;
  16.     cout<<"花費: "<<clock()<<" 毫秒"<<endl;
  17.     return 0;
  18. }
複製代碼

返回列表