算法笔记 — 数列

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

题目链接:http://codeup.cn/problem.php?cid=100000583&pid=1

题目描述

编写一个求斐波那契数列的递归函数,输入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>
using namespace std;
int f(int n){
	if(n==1){
		return 1;
	}else if(n==2){
		return 1;
	}else{
		return f(n-1)+f(n-2);
	}
}
int main(){
	int T,n;
	cin>>T;
	while(T--){
		cin>>n;
		for(int i=0;i<n;i++){
			for(int j=1;j<=(n-i-1)*2;j++){
				cout<<" ";
			}
			cout<<"0";
			for(int j=1;j<=i*2;j++){
				cout<<" "<<f(j);
			}
			cout<<endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37345402/article/details/84591372