您的位置:首页 > 编程语言 > C语言/C++

fibonacci数列C++语言多种实现

2015-12-20 00:24 417 查看
转载时请注明出处和作者联系方式

文章出处:http://blog.csdn.net/chenchong08

作者联系方式:vision_chen@yeah.net

斐波纳契数列(Fibonacci Sequence),又称黄金分割数列,指的是这样一个数列:

1、1、2、3、5、8、13、21、... ...

在数学上,斐波纳契数列以如下被以递归的方法定义:f(0)=1, f(1)=1, f(n)=f(n-1)+f(n-2)(n>=2,n∈N*).

[cpp] view
plaincopyprint?

<span style="font-size:16px;">#include <cstdio>

#include <cstdlib>

#include <vector>

#include <queue>

#include <cmath>

using namespace std;

/*

* fibonacci的递归函数解法

* 优点:代码简单,易于理解

* 缺点:fib参数参数比较大时,运算量很大

*/

unsigned long long Fib_Recursive( unsigned int fib )

{

if ( fib > 2 ) return ( Fib_Recursive(fib-1) + Fib_Recursive(fib-2) );

else if ( 2 == fib ) return 2;

else if ( 1 == fib ) return 1;

return 1;

}

/*

* fibonacci的数组解法

* 优点:代码比较简单,也比较容易理解

* 缺点:fib多大就new多大的内存

*/

unsigned long long Fib_Array( unsigned int fib )

{

unsigned long long result = 1;

if ( fib > 2 )

{

unsigned long long *pArray = NULL;

pArray = new unsigned long long[fib];

pArray[0] = 1;

pArray[1] = 2;

unsigned int loop;

for ( loop = 2 ; loop < fib; ++loop )

{

pArray[loop] = pArray[loop-1] + pArray[loop-2];

}

result = pArray[loop-1];

delete []pArray;

}

else if ( 2 == fib ) return 2;

else if ( 1 == fib ) return 1;

return result;

}

/*

* vector实现

*/

unsigned long long Fib_Vector( unsigned int fib )

{

if ( fib > 2 )

{

vector<unsigned long long> resultVec;

resultVec.reserve( fib );

resultVec.push_back(1);

resultVec.push_back(2);

unsigned int i;

for ( i = 2; i < fib; ++i )

{

resultVec.push_back( resultVec[i-1]+resultVec[i-2] );

}

return resultVec[i-1];

}

else if ( 2 == fib ) return 2;

else if ( 1 == fib ) return 1;

return 1;

}

/*

* queue实现

*/

unsigned long long Fib_Queue( unsigned int fib )

{

if ( fib > 2 )

{

queue<unsigned long long> resultQueue;

resultQueue.push(1);

resultQueue.push(2);

for (unsigned int i = 2; i < fib; ++i )

{

resultQueue.push(resultQueue.front() + resultQueue.back());

resultQueue.pop();

}

return resultQueue.back();

}

else if ( 2 == fib ) return 2;

else if ( 1 == fib ) return 1;

return 1;

}

/*

* 迭代实现

*/

unsigned long long Fib_Iteration( unsigned int fib )

{

if ( fib > 2 )

{

unsigned long long a1 = 1, a2 = 2, a3 = 0;

unsigned int i;

for (i=0; i<fib-2; ++i)

{

a3 = a1 + a2;

a1 = a2;

a2 = a3;

}

return a3;

}

else if ( 2 == fib ) return 2;

else if ( 1 == fib ) return 1;

return 1;

}

/*

* 公式实现

* 缺点:有误差

*/

unsigned long long Fib_Formula( unsigned int fib )

{

if ( fib > 0 )

{

double square_root_5=sqrt((double)5);

return (pow((1+square_root_5),(double)(fib+1))-pow((1-square_root_5),(double)(fib+1)))/(pow((double)2,(double)(fib+1))*square_root_5);

}

return 1;

}

int main(int argc, char **argv)

{

unsigned int i;

for ( i = 0; i <= 20; ++i )

{

printf(" %lld\t%lld\t%lld\t%lld\t%lld\t%lld\n",Fib_Recursive(i),Fib_Array(i),Fib_Vector(i),Fib_Queue(i),Fib_Iteration(i),Fib_Formula(i));

}

#ifdef _WIN32

system("pause");

#endif

return 0;

}</span>

VS2012运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: