# 【算法02】3种方法求解斐波那契数列

 1 #include<iostream> 2 #include<string> 3 using namespace std; 4  5 long Fibonacci(unsigned int n) 6 { 7     if(n == 0) 8         return 0; 9     else if(n == 1)10         return 1;11     else12         return Fibonacci(n-1) + Fibonacci(n-2);13 }14 15 int main()16 {17     cout<<"Enter An N:"<<endl;18     unsigned int number=0;19     cin>>number;20     cout<<Fibonacci(number)<<endl;21     return 0;22 }

 1 #include<iostream> 2 #include<string> 3 using namespace std; 4  5 long Fibonacci(unsigned int n) 6 { 7     if(n == 0) 8         return 0; 9     if(n == 1)10         return 1;11     long firstItem = 0;12     long secondItem = 1;13     long fib = 0;14     unsigned int cnt = 1;15     while(cnt < n)16     {17         fib = firstItem + secondItem;18         firstItem = secondItem;19         secondItem = fib;20         ++cnt;21     }22     return fib;23 }24 25 int main()26 {27     cout<<"Enter A Number:"<<endl;28     unsigned int number;29     cin>>number;30     cout<<Fibonacci(number)<<endl;31     return 0;32 }

Step1: n=2

Step2：设n=k时，公式成立，则有：

=右，这正是n=k+1时的形式，即当n=k+1时等式成立。

Step1Step2可知，该数学公式成立。

 1 #include<iostream> 2 #include<string> 3 using namespace std; 4  5 //定义2×2矩阵； 6 struct Matrix2by2 7 { 8     //构造函数 9     Matrix2by210     (11         long m_00,12         long m_01,13         long m_10,14         long m_1115     )16     :m00(m_00),m01(m_01),m10(m_10),m11(m_11)17     {18     }19 20     //数据成员21     long m00;22     long m01;23     long m10;24     long m11;25 };26 27 //定义2×2矩阵的乘法运算28 Matrix2by2 MatrixMultiply(const Matrix2by2& matrix1,const Matrix2by2& matrix2)29 {30     Matrix2by2 matrix12(1,1,1,0);31     matrix12.m00 = matrix1.m00 * matrix2.m00 + matrix1.m01 * matrix2.m10;32     matrix12.m01 = matrix1.m00 * matrix2.m01 + matrix1.m01 * matrix2.m11;33     matrix12.m10 = matrix1.m10 * matrix2.m00 + matrix1.m11 * matrix2.m10;34     matrix12.m11 = matrix1.m10 * matrix2.m01 + matrix1.m11 * matrix2.m11;35     return matrix12;36 37 }38 39 40 //定义2×2矩阵的幂运算41 Matrix2by2 MatrixPower(unsigned int n)42 {43     Matrix2by2 matrix(1,1,1,0);44     if(n == 1)45     {46         matrix = Matrix2by2(1,1,1,0);47     }48     else if(n % 2 == 0)49     {50         matrix = MatrixPower(n / 2);51         matrix = MatrixMultiply(matrix, matrix);52     }53     else if(n % 2 == 1)54     {55         matrix = MatrixPower((n-1) / 2);56         matrix = MatrixMultiply(matrix, matrix);57         matrix = MatrixMultiply(matrix, Matrix2by2(1,1,1,0));58     }59     return matrix;60 }61 //计算Fibnacci的第n项62 long Fibonacci(unsigned int n)63 {64     if(n == 0)65         return 0;66     if(n == 1)67         return 1;68 69     Matrix2by2 fibMatrix = MatrixPower(n-1);70     return fibMatrix.m00;71     72 }73 74 int main()75 {76     cout<<"Enter A Number:"<<endl;77     unsigned int number;78     cin>>number;79     cout<<Fibonacci(number)<<endl;80     return 0;81 }

1）本博客所有的代码环境编译均为win7+VC6。所有代码均经过博主上机调试。

2）博主python27对本博客文章享有版权，网络转载请注明出处对解题思路有任何建议，欢迎在评论中告知。

posted @ 2011-11-25 00:38  python27  阅读(20809)  评论(4编辑  收藏