费氏数列的全称为费波那契数列,指的是这样一个数列1、1、2、3、5、8、13、21、34….. .这个数列的第一项和第二项均为1,从第三项开始,每一项都等于前两项之和。由用户输入需要计算的费氏序列的项数(项数从1开始).然后程序算出费氏序列中该项的数值,并打印出来
#include <iostream>
using namespace std;
int64_t Fib(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return Fib(n - 1) + Fib(n - 2);
}
int main(int argc, char const *argv[])
{
for (int i = 0; i < 100; i++)
{
cout << "Fib(" << i << ")"
<< "=" << Fib(i) << endl;
}
return 0;
}