#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; } }
原文地址:http://blog.csdn.net/huolang_vip/article/details/45195363