问题 E: 杨辉三角
内存限制:128 MB
时间限制:1.000 S
评测方式:文本比较
命题人:
提交:8352
解决:2235
题目描述
中国古代数学家在数学的重要领域中,处于遥遥领先的地位,杨辉三角形的发现就是十分精彩的一页!杨辉三角形由n排数字组成,第n排含有n个数字,每排数字的首末为1,其他数是其肩上两数之和,即:第n排第j个数的数值等于第n-1排第j-1和j两数数值之和.
输入
每行是一个测试用例,输入一个所需的行数n,n为大于0小于等于30的整数。
输出
输出杨辉三角的前n行,每行输出的第一个数前面没有空格,以后的每个数据的位宽为杨辉三角中最大数的宽度加上一,每组输出数据间用换行符隔开。
样例输入 复制
7
5
样例输出 复制
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
提示
C++ cout 设置输出位宽需要用到如下2个函数:
- setw(int):设置宽度
- setfill(char):设置填充的字符串
都在iomanip的头文件中。
建议使用链式队列解决该问题,代码框架如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
using namespace std;
struct Node{
int data;
Node* next;
};
struct Queue{
Node* head,*tail;
};
void InitQueue(Queue& Q){
}
int IsQueueEmpty(const Queue& Q){
}
int Top(Queue& Q){
if(IsQueueEmpty(Q)){
cout << "error: top, empty queue" << endl;
return 0;
}
}
int EnQueue(Queue& Q, int data){
return 0;
}
int DeQueue(Queue& Q){
if(IsQueueEmpty(Q)){
cout << "error: dequeue, empty queue" << endl;
return 0;
}
}
void DestroyQueue(Queue& Q){
}
void PrintQueue(const Queue& Q, int maxlen){
Node* p = Q.head;
int isfirst = 1;
while(p != nullptr){
if(p->data != 0){
if(isfirst) cout << p->data << " ";
else cout << setw(maxlen) << p->data << " ";
isfirst = 0;
}
p = p->next;
}
cout << endl;
}
int getMaxNumber(int num){
}
void PrintYHTraingle(int num){
}
int main(){
// freopen("/config/workspace/answer/DataStructure/test.in","r",stdin);
int num;
while(cin >> num){
PrintYHTraingle(num);
}
return 0;
}