题目1075:斐波那契数列
时间限制:
5 秒
内存限制:
32 兆
特殊判题:
否
提交:
1942
解决:
1094
题目描述:
编写一个求斐波那契数列的递归函数,输入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
来源:
2002年清华大学计算机研究生机试真题(第II套)
//九度oj1075
//
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int fibonacci(int x)
{
if(x == 0)
return 0;
else
if(x == 1)
return 1;
else
return fibonacci(x-1) + fibonacci(x-2) ;
}
int main()
{
int n;
while(cin >> n)
{
for(int i = 1; i <= n; i++)
{
for(int j = 0; j < 2*i-2; j++)
cout << fibonacci(j) << " ";
cout << fibonacci(2*i-2);
cout << endl;
}
}
return 0;
}
/**************************************************************
Problem: 1075
User: true14fans
Language: C++
Result: Accepted
Time:2850 ms
Memory:1520 kb
****************************************************************/