- 题目描述:
-
编写一个求斐波那契数列的递归函数,输入n值,使用该递归函数,输出如样例输出的斐波那契数列。
- 输入:
-
一个整型数n
- 输出:
-
题目可能有多组不同的测试数据,对于每组输入数据,
按题目的要求输出相应的斐波那契图形。
- 样例输入:
-
6
- 样例输出:
-
0 0 1 1 0 1 1 2 3 0 1 1 2 3 5 8 0 1 1 2 3 5 8 13 21 0 1 1 2 3 5 8 13 21 34 55
#include <iostream>
using namespace std;
int f(int n)
{
//int c;
if (n==1)
{
//cout<<1<<" ";
return 1;
}
if (n==0)
{
//cout<<0<<" ";
return 0;
}
if (n==2)
{
//cout<<1<<" ";
return 1;
}
else
{
//c=f(n-1)+f(n-2);
//cout<<f(n-1)+f(n-2)<<endl;
return f(n-1)+f(n-2);
}
}
int main()
{
int n;
int c;
int i,j;
while (cin>>n)
{
for (i=0;i<n;i++)
{
for (j=0;j<2*i+1;j++)
{
cout<<f(j);
if (j!=2*i)
{
cout<<" ";
}
}
cout<<endl;
}
}
return 1;
}
/**************************************************************
Problem: 1075
User: Carvin
Language: C++
Result: Accepted
Time:1920 ms
Memory:1520 kb
****************************************************************/