九度oj 1075

来源:互联网 发布:阿里巴巴商机助理 mac 编辑:程序博客网 时间:2024/06/10 10:03
题目描述:

编写一个求斐波那契数列的递归函数,输入n值,使用该递归函数,输出如样例输出的斐波那契数列。

输入:

一个整型数n

输出:

题目可能有多组不同的测试数据,对于每组输入数据,
按题目的要求输出相应的斐波那契图形。

样例输入:
6
样例输出:
00 1 10 1 1 2 30 1 1 2 3 5 80 1 1 2 3 5 8 13 210 1 1 2 3 5 8 13 21 34 55
来源:

2002年清华大学计算机研究生机试真题(第II套)

#include<iostream>#include<stdio.h>using namespace std;int fib(int x){    if(x==0)    {            return 0;            }            if(x==1||x==2)            {                          return 1;                          }                          if(x>=3)                          {                                  return fib(x-1)+fib(x-2);                                  }                                  }                                  int main(){    int n;    while(cin>>n)    {                 for(int j=0;j<=2*n-2;j=j+2)                 {                                 for(int i=0;i<j;i++)        {                         cout<<fib(i)<<" ";                         }                         cout<<fib(j)<<endl;                         }                         }                         }


0 0