杨辉三角
先上运行结果(显示前n行)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4ce1f1206cb3cbb52495f72d7e79de89.png#pic_center)
源代码如下
#include<iostream>
using namespace std;
class Node {
public:
int e;
Node* next;
Node(int x,Node* n=NULL):e(x),next(n){}
Node():next(NULL){}
};
class Queue{
public:
Queue() { front = rear = NULL; }
~Queue() { while (front != NULL) { Node* temp = front; front = front->next; delete temp; } }
void inqueue(int it);
void outqueue(int& it);
private:
Node* front, * rear;
};
void Queue::inqueue(int it) {
if (front == NULL) { front = rear = new Node(it); return; }
Node* temp = new Node(it);
rear->next = temp;
rear = temp;
}
void Queue::outqueue(int& it) {
if (front == NULL) { cout << "false" << endl; return; }
Node* temp = front;
front = front->next;
if (front == NULL) { rear = NULL; }
it = temp->e;
delete temp;
}
void print(int n) {
Queue queue;
queue.inqueue(1);
cout << '1' << endl;
int s = 0;
for (int i = 2; i <= n; i++) {
queue.inqueue(0);
for (int j = 0; j < i; j++) {
int t;
queue.outqueue(t);
s = s + t;
cout << s << '\t';
queue.inqueue(s);
s = t;
}
cout << endl;
}
}
int main() {
int n=5;
print(n);
return 0;
}