您的位置:首页 > Web前端

剑指offer——斐列那契数列(递归)

2016-07-03 14:11 330 查看
斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368。

这个数列从第2项开始,每一项都等于前两项之和。

“test.c”

<span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;

int Fibonacci(int n)
{
if (n > 1)
{
return Fibonacci(n-1)+Fibonacci(n-2);
}
else if (n == 1)
{
return 1;
}
else//n == 0
{
return 0;
}
}

void test()
{
int n = 0;
cin>>n;
int total = 0;

for (int i = 0;i <= n;i++)
{
total += Fibonacci(i);
cout<<"Fibonacci("<<i<<") = "<<Fibonacci(i)<<endl;
}
cout<<"total = "<<total<<endl;
}

int main()
{
test();
system("pause");
return 0;
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: