面试题9:斐波那契数列
题目1:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项。斐波那契数列的定义如下:
预备知识:
在需要重复地多次计算相同的问题,通常可以选择递归或循环(迭代)两种不同方法。
递归(recursive):在一个函数内部调用这个函数自身。可以通过栈实现的,本质是把一个问题分解成两个或多个小问题。终止条件很重要,特点是代码比较简单,缺点:时间和空间消耗大,且可能导致调用栈溢出。
循环(iterative):通过设置计算的初始值及终止条件,在一个范围内重复运算。
思路:1.利用递归实现,2利用循环迭代实现
算法实现:
#include "stdafx.h"
// ====================方法1:递归====================
long long Fibonacci_Solution1(unsigned int n)
{
if(n <= 0)
return 0;
if(n == 1)
return 1;
return Fibonacci_Solution1(n - 1) + Fibonacci_Solution1(n - 2); //递归实现
}
// ====================方法2:循环====================
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:基于矩阵乘法====================
#include <cassert>
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)
{
}
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)
{
assert(n > 0);
Matrix2By2 matrix;
if(n == 1)
{
matrix = Matrix2By2(1, 1, 1, 0);
}
else if(n % 2 == 0)
{
matrix = MatrixPower(n / 2);
matrix = MatrixMultiply(matrix, matrix);
}
else if(n % 2 == 1)
{
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", n);
else
printf("Test for %d in solution1 failed.\n", n);
if(Fibonacci_Solution2(n) == expected)
printf("Test for %d in solution2 passed.\n", n);
else
printf("Test for %d in solution2 failed.\n", n);
if(Fibonacci_Solution3(n) == expected)
printf("Test for %d in solution3 passed.\n", n);
else
printf("Test for %d in solution3 failed.\n", n);
}
int _tmain(int argc, _TCHAR* 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);
return 0;
}
题目:2:一只青蛙一次可以跳1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶共有多少种跳法。
思路:这是一个斐波那契数列的一个应用。
/**********************************7月13日******************************/
// 面试题9.cpp : 定义控制台应用程序的入口点。
//
//斐波那契数列
#include "stdafx.h"
//递归实现
long long Fibonacci_1(unsigned int n)
{
if(n <= 0)
return 0;
if(n == 1)
return 1;
return Fibonacci_1(n -1) + Fibonacci_1(n - 2);
}
//循环实现
long long Fibonacci_2(unsigned int n)
{
int result[2] = {0, 1};
if (n < 2) //0,1
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;
}
/*****************测试********************/
void Test1()
{
long long result = Fibonacci_1(3);
printf("%ld\n", result);
}
void Test2()
{
long long result = Fibonacci_2(3);
printf("%ld\n", result);
}
int _tmain(int argc, _TCHAR* argv[])
{
Test1();
Test2();
return 0;
}