// 面试题:斐波那契数列 // 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项。 #include <iostream> using namespace std; // ====================方法1:递归==================== //注意这种递归方法虽然看起来很简单,但是由于压入栈和弹出,会存在栈溢出的可能,而且效率特别慢,且n越大效率越慢 long long Fibonacci_Solution1(unsigned int n)//注意long long { if (n <= 0) return 0; if (n == 1) return 1; return Fibonacci_Solution1(n - 1) + Fibonacci_Solution1(n - 2); } // ====================方法2:循环==================== //这是一种简单的方法,时间复杂度O(n),值得提倡 long long Fibonacci_Solution2(unsigned n) { int result[2] = { 0, 1 };//注意头两个数我们无法计算,直接给出 if (n < 2) return result[n]; long long fibNMinusOne = 1; long long fibNMinusTwo = 0; long long fibN = 0; for (unsigned int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; } // ====================方法3:基于矩阵乘法==================== //不常见但是时间复杂度更低: O(logn) struct Matrix2By2 { Matrix2By2(long long m00 = 0, long long m01 = 0, long long m10 = 0, long long m11 = 0) { m_00 = m00; m_01 = m01; m_10 = m10; m_11 = m11; } //Matrix2By2(long long m00 = 0, long long m01 = 0, long long m10 = 0, long long m11 = 0) :m_00(m00), m_01(m01), m_10(m10), m_11(m11) {} //也可以写成上面这个形式,都是含参具有默认值的构造函数,是参数列表外初始化 long long m_00; long long m_01; long long m_10; long long m_11; }; Matrix2By2 MatrixMultiply(const Matrix2By2& matrix1,const Matrix2By2& matrix2)//矩阵乘法 { return Matrix2By2( matrix1.m_00 * matrix2.m_00 + matrix1.m_01 * matrix2.m_10, matrix1.m_00 * matrix2.m_01 + matrix1.m_01 * matrix2.m_11, matrix1.m_10 * matrix2.m_00 + matrix1.m_11 * matrix2.m_10, matrix1.m_10 * matrix2.m_01 + matrix1.m_11 * matrix2.m_11); } Matrix2By2 MatrixPower(unsigned int n) { Matrix2By2 matrix; if (n == 1)//第一种情况:n=1,返回矩阵(1, 1, 1, 0) { matrix = Matrix2By2(1, 1, 1, 0); } else if (n % 2 == 0)//第二种情况:n为偶数,递归求a^(n/2),然后乘回来 { matrix = MatrixPower(n / 2); matrix = MatrixMultiply(matrix, matrix); } else if (n % 2 == 1)//第三种情况:n为奇数数,用第二种情况递归求a^((n-1)/2),然后乘回来后,再乘一次(1, 1, 1, 0) { matrix = MatrixPower((n - 1) / 2); matrix = MatrixMultiply(matrix, matrix); matrix = MatrixMultiply(matrix, Matrix2By2(1, 1, 1, 0)); } return matrix; } long long Fibonacci_Solution3(unsigned int n) { int result[2] = { 0, 1 };//注意头两个数我们无法计算,直接给出 if (n < 2) return result[n]; Matrix2By2 PowerNMinus2 = MatrixPower(n - 1); return PowerNMinus2.m_00; } // ====================测试代码==================== void Test(int n, int expected) { if (Fibonacci_Solution1(n) == expected) printf("Test for %d in solution1 passed. ", n); else printf("Test for %d in solution1 failed. ", n); if (Fibonacci_Solution2(n) == expected) printf("Test for %d in solution2 passed. ", n); else printf("Test for %d in solution2 failed. ", n); if (Fibonacci_Solution3(n) == expected) printf("Test for %d in solution3 passed. ", n); else printf("Test for %d in solution3 failed. ", n); } int main(int argc, char* argv[]) { Test(0, 0); Test(1, 1); Test(2, 1); Test(3, 2); Test(4, 3); Test(5, 5); Test(6, 8); Test(7, 13); Test(8, 21); Test(9, 34); Test(10, 55); Test(40, 102334155); system("pause"); }
第三种想法思路:
斐波那契数列扩展: