Fibonacci数列主要是了解递归的基本格式,即递归边界+递归式的顺序,请牢记.
递归式是F(n)=F(n-1)+F(n-2)(n>=2)
代码示例如下:
#include<iostream>
#include<cstdlib>
using namespace std;
int F(int n);
int main(void)
{
int n;
cin >> n;
cout << F(n) << endl;
system("pause");
return 0;
}
int F(int n)
{
if (n == 0 || n == 1) return 1;//递归边界
else return F(n - 1) + F(n - 2);//递归式
}