队列练习——杨辉三角
【摘要】 杨辉三角
杨辉三角,是二项式系数在三角形中的一种几何排列。
C++代码实现
/*
队列————杨辉三角
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR -1
#define OVERFLOW -2
type...
杨辉三角
- 杨辉三角,是二项式系数在三角形中的一种几何排列。
C++代码实现
/*
队列————杨辉三角
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
#define MAXSIZE 100
typedef struct Qnode {
QElemType data;
struct Qnode* next;
}Qnode, * QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitLQueue(LinkQueue& Q) {
Q.front = new Qnode;
if (!Q.front) exit(OVERFLOW);
Q.rear = Q.front;
Q.front->next = NULL;
return OK;
}
// 判断链队列是否为空
bool LQueueEmpty(LinkQueue Q) {
return (Q.front == Q.rear);
}
// 入队
Status PushLQueue(LinkQueue& Q, QElemType e) {
QueuePtr q;
q = new Qnode;
if (!q) exit(OVERFLOW);
q->data = e;
q->next = NULL;
Q.rear->next = q;
Q.rear = q;
return OK;
}
// 出队
Status PopLQueue(LinkQueue& Q, QElemType& e) {
QueuePtr q;
if (LQueueEmpty(Q)) return ERROR;
q = Q.front->next;
e = q->data;
Q.front->next = q->next;
/*
if (Q.rear == q) Q.rear = Q.front;
*/
if (Q.rear == q) Q.rear = Q.front;
delete q;
return OK;
}
// 销毁链队列
Status DestroyQueue(LinkQueue& Q) {
while (Q.front) {
Q.rear = Q.front->next;
delete Q.front;
Q.front = Q.rear;
}
return OK;
}
// 获取队头元素
int GetHead(LinkQueue Q) {
QElemType e;
if (LQueueEmpty(Q)) return 0;
e = Q.front->next->data;
return e;
}
// 创建链队列
void CreateLQueue(LinkQueue& Q, int m) {
QElemType e;
for (int i = 1; i <= m; i++) {
cout << "请输入第" << i << "个元素: ";
cin >> e;
PushLQueue(Q, e);
}
}
// 输出链队列
void OutPut(LinkQueue Q) {
QueuePtr q;
q = new Qnode;
q = Q.front->next;
while (q) {
cout << q->data << " ";
q = q->next;
}
cout << endl;
}
void f() {
cout << "请输入杨辉三角的阶数: ";
int num;
cin >> num;
if (num == 1) // 行数为1
cout << 1 << endl;
else {
cout << '1' << endl;
cout << "1 1" << endl;
LinkQueue q1; // 存储第i层数据
InitLQueue(q1);
QElemType e, q;
// 第二行两个1
for (int i = 0; i < 2; i++) PushLQueue(q1, 1);
for (int i = 0; i < num - 2; i++) { LinkQueue q2; // 存储第 i + 1 层 InitLQueue(q2); PushLQueue(q2, 1); // 第一个数是1 while (!LQueueEmpty(q1)) { PopLQueue(q1, q); if (LQueueEmpty(q1)) PushLQueue(q2, 1); else PushLQueue(q2, q + GetHead(q1)); } q1 = q2; OutPut(q1);
}
}
}
int main()
{
f();
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
请输入杨辉三角的阶数: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
- 1
- 2
- 3
- 4
- 5
- 6
文章来源: ruochen.blog.csdn.net,作者:若尘,版权归原作者所有,如需转载,请联系作者。
原文链接:ruochen.blog.csdn.net/article/details/102903632
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)