swust oj上的循环队列输出杨辉三角,用的STL里的容器
打印杨辉三角形
1000(ms)
10000(kb)
3212 / 16113
杨辉三角形具有如下特征:
1、每行数字左右对称,由1开始逐渐变大,然后变小,回到1。
2、第n行的数字个数为n个。
3、第n行数字和为2^(n-1)。
4、每个数字等于上一行的左右两个数字之和。可用此性质写出整个帕斯卡三角形。
5、将第2n+1行第1个数,跟第2n+2行第3个数、第2n+3行第5个数……连成一线,这些数的和是第2n个斐波那契数。将第2n行第2个数,跟第2n+1行第4个数、第2n+2行第6个数……这些数之和是第2n-1个斐波那契数。
6、第n行的第1个数为1,第二个数为1×(n-1),第三个数为1×(n-1)×(n-2)/2,第四个数为1×(n-1)×(n-2)/2×(n-3)/3…依此类推。
例如7阶杨辉三角形如下:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
要求:利用循环队列,编程实现打印杨辉三角形。(n在0-12之间取值)
输入
输入要打印的杨辉三角形的阶n。
输出
打印出n阶杨辉三角形。
样例输入
7
样例输出
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
提 交
gcc
#include
#include
#include
using namespace std;
int main()
{
queueq;
int n;
cin>>n;
int a=1,b=0;
q.push(a);
q.push(b);
int temp1,temp2,temp3;
if(n>1){
cout<<“1”<<" “<<endl;
while(n-1>0){
n–;
int mark=0,num=q.front();
while(num!=0){
if(mark0)
cout<<q.front()<<" “;
temp1=q.front();
q.pop();
temp2=q.front() ;
temp3=temp2+temp1;
cout<<temp3<<” ";
if(mark0)
q.push(a);
mark=1;
q.push(temp3);
num=q.front();
}
q.pop();
q.push(b);
num=q.front();
cout<<endl;
}
}
else
cout<<“1”<<” "<<endl;
}