打印杨辉三角(STL版队列)

#include <iostream>
#include <queue>
using namespace std;

void YangHuiTriangle(int n);
int main()
{
	cout<<"请输入杨辉三角的层数:";
	int x;
	cin>>x; 
    YangHuiTriangle(x);
	return 0;
}

void YangHuiTriangle(int n)
{
	queue<int> q;
	q.push(0);
	q.push(1);
	
	int x,y; 
	for(int i=0;i<n;i++)
	{
		q.push(0);
		for(int j=n;j>i;j--)
		cout<<" ";
		do
		{
			x = q.front();
			q.pop();
			
			y = q.front();
			
			y!=0? cout<<y<<" " : cout<<" " ;
			q.push((x+y));
		}while(y!=0);
		cout<<endl; 
	}
}

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/huolang_vip/article/details/45195363
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞