循环队列实现杨辉三角C++
时间: 2023-10-28 22:07:07 浏览: 138
循环队列实现杨辉三角
4星 · 用户满意度95%
循环队列实现杨辉三角的C++代码如下:
```c++
#include <iostream>
using namespace std;
const int MAXN = 100;
int q[MAXN];
int front = 0, rear = 0;
void enqueue(int x) {
q[rear] = x;
rear = (rear + 1) % MAXN;
}
int dequeue() {
int x = q[front];
front = (front + 1) % MAXN;
return x;
}
int main() {
int n;
cout << "请输入杨辉三角的行数:";
cin >> n;
enqueue(1);
for (int i = 1; i <= n; i++) {
int last = 0;
for (int j = 0; j < i; j++) {
int x = dequeue();
cout << x << " ";
enqueue(x + last);
last = x;
}
cout << endl;
enqueue(1);
}
return 0;
}
```
阅读全文