用队列打印杨辉三角

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
queue<int> Q;
int temp;
void tringle(const int n)
{
    Q.push(1);
    for(int i=2;i<=n;i++)
    {
        Q.push(1);
        for(int j=1;j<=i-2;j++)
        {
            temp=Q.front();
            Q.pop();
            printf(" %d",temp);
            temp=temp+Q.front();
            Q.push(temp);
        }
        int x=Q.front();
        Q.pop();
        printf(" %d
",x);
        Q.push(1);
    }
    while(!Q.empty())
    {
        temp=Q.front();
        Q.pop();
        printf(" %d",temp);
    }
}
int main()
{
    tringle(6);
    return 0;
}

原文地址:https://www.cnblogs.com/jianfengyun/p/3885122.html