返回列表 發帖

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

費氏數列 - 維基百科

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

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

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

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. 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. fai(5)
  13. =fai(3)+fai(4)
  14. =fai(1)+fai(2)+fai(2)+fai(3)
  15. =1+fai(0)+fai(1)+fai(0)+fai(1)+fai(1)+fai(2)
  16. =1+0+1+0+1+1+fai(0)+fai(1)
  17. =1+0+1+0+1+1+0+1
  18. =5
  19. */
  20. int main()
  21. {
  22.     int n;
  23.     cout<<"請問要推算費氏數列到第幾項次? ";
  24.     cin>>n;
  25.     for(int i=0; i<=n; i++)
  26.         cout<<fai(i)<<" ";
  27.     cout<<endl;
  28.     system("pause");
  29.     return 0;
  30. }
複製代碼
May

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int f(int x)
  4. {
  5.     if(x<2)
  6.         return x;
  7.     else
  8.         return f(x-2)+f(x-1);
  9. }
  10. int main()
  11. {
  12.     int x;
  13.     cout<<"要推算到第幾項次? ";
  14.     cin>>x;
  15.     for(int i=0; i<=x; i++)
  16.         cout<<"第"<<i<<"項次為:"<<f(i)<<endl;
  17.     cout<<endl;
  18.     system("pause");
  19.     return 0;
  20. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. {
  4.     int f(int n);
  5.     if(n==0 || n==1)
  6.         return n;
  7.     else
  8.         return f(n-2)+f(n-1);

  9. }
  10. int main()
  11. {
  12.     int n;
  13.     cout<<"請問要推算費氏數列到第幾次?";
  14.     cin>>n;
  15.     for(int i=0;i<=n;i++)
  16.         cout<<f(i)<<" ";
  17.     return 0;
  18. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int a(int n)
  5. {
  6.    if (n==0)
  7.       return 0;
  8.    else if(n==1)
  9.       return 1;
  10.    return a(n-1)+a(n-2);
  11. }
  12. int main()
  13. {
  14.     int n;
  15.     cin>>n;
  16.     cout<<a(n);
  17. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int fai(int n)
  5. {
  6.     if(n<2)    //邊界條件
  7.         return n;
  8.     else
  9.         return fai(n-2)+fai(n-1);
  10. }
  11. int main()
  12. {
  13.     int n;
  14.     cout<<"請問要推算費氏數列到第幾項次? ";
  15.     cin>>n;
  16.     for(int i=0; i<=n; i++)
  17.         cout<<fai(i)<<" ";
  18.     cout<<endl;
  19.     system("pause");
  20.     return 0;
  21. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a(int n)
  4. {
  5.     if(n==0)
  6.         return 0;
  7.     else if(n==1)
  8.         return 1;
  9.     return a(n-1)+a(n-2);
  10. }
  11. int main()
  12. {
  13.     int n;
  14.     cin>>n;
  15.     cout<<a(n);
  16.     return 0;
  17. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. {
  4.     int f(int n);
  5.     if(n==0 || n==1)
  6.         return n;
  7.     else
  8.         return f(n-2)+f(n-1);

  9. }
  10. int main()
  11. {
  12.     int n;
  13.     cout<<"請問要推算費氏數列到第幾次?";
  14.     cin>>n;
  15.     for(int i=0;i<=n;i++)
  16.         cout<<f(i)<<" ";
  17.     return 0;
  18. }
複製代碼

TOP

返回列表