C++递归斐波那契数列
#include <iostream>
using namespace std;
int Fibonacci(int);//函数原型
void main() {
int a;
cout << "请输入要递归的斐波那契数列的层数" << endl;
cin >> a;
cout << "-----------" << endl;
int b = Fibonacci(a);
cout << endl << b;
}
int Fibonacci(int a) {//011235
static int x = 0;
static int y = 1;
if (x == 0)
{
cout << x << " " << y << " ";//输出第一个值
}
if (a - 2 != 1)
{
x += y;
y += x;
cout << x << " " << y << " ";//输出
}
else
{
cout << (x += y) << " ";//输出单数值
}
if (a - 2 > 2)
{
Fibonacci(a - 2);//递归
}
else
{
return 0;//返回
}
}
效果图: