斐波那契数列指的是这样一个数列 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>