#include<bits/stdc++.h>
using namespace std;
void yanhui(int bnum){
queue<int> Q; //用于生成杨辉三角数规律的队列
queue<int> my; //用于保存生成好的杨辉三角数
Q.push(1); //第一行元素入队
for(int i=2;i<=bnum;i++){ //产生第n行元素并入队Q,同时把生成的好的数放入队列my
Q.push(1);
for(int j=1;j<=i-2;j++){//生成第n行中间n-2个数字
int temp=Q.front();
my.push(temp); //从Q中弹出的数字是已生成好的,用my接收
Q.pop();
int x=Q.front();
Q.push(temp+x);
}
int x2=Q.front();
my.push(x2);
Q.pop();
Q.push(1); //第n行最后一个元素入队
}
while(Q.empty()==false){ //第n行的数字放入my中
int x3=Q.front();
my.push(x3);
Q.pop();
}
int count=0,line=1;
while(my.empty()==false){//打印三角形
int x4=my.front();
cout<<x4<<" ";
my.pop();
count++;
if(count==line){
cout<<endl;
count=0;
line++;
}
}
}
int main()
{
yanhui(7);//打印7行的三角形
}
新手上路,代码可能有冗余,欢迎参考学习 批评指正