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运行结果:
文章出处: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运行结果:
![](http://my.csdn.net/uploads/201207/22/1342968550_5929.jpg)
相关文章推荐
- error: Microsoft Visual C++ 9.0 is required (Unabl
- 2.变量和基本类型(c++)
- 1.快速入门(c++)
- C++(VS2012)DLL动态库的生成和调用
- c++ string 转 char* const char* char[]
- C语言SOCKET学习
- c语言的日常心得与总结
- 张氏读写锁,c++信号量实现
- C++:顺序容器及顺序容器适配器(stack、queue等)
- 关于C++二分搜索树的中序遍历和先序遍历
- C++:函数
- c语言常用小知识点(const、sizeof)
- c++中io流的使用
- c++匿名类—指针
- 黑马程序员-C语言-约瑟夫环
- c++静态成员与静态函数
- c++基础语法 构造函数 析构函数 类的组合
- C语言程序设计进阶 第一周1.1笔记
- c语言随机数
- C++中的inline声明