codeup 2018 Problem B数列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Hacker_Wind/article/details/81707779

问题 B: 数列

时间限制: 1 Sec  内存限制: 32 MB
提交: 325  解决: 182
[提交][状态][讨论版][命题人:外部导入]

题目描述

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

输入

输入第一行为样例数m,接下来有m行每行一个整数n,n不超过10。

输出

对应每个样例输出要求的图形(参见样例格式)。

样例输入

1
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>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
using namespace std;
int n;
int solve(int a)
{
    if(a==0)
    {
        return 0;
    }
    else if(a==1)
    {
        return 1;
    }
    else
    {
        return solve(a-1)+solve(a-2);
    }
}
int main()
{
    int m;
    cin>>m;
   // cout<<solve(3)<<endl;
   while(m--)
   {
    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<(n-1-i)*2;j++)
            cout<<" ";
            cout<<"0";
        for(int k=1;k<i*2+1;k++)
        cout<<" "<<solve(k);
        cout<<endl;
    }
   }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Hacker_Wind/article/details/81707779