西南科技大学oj题,用循环队列打印杨辉三角

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(mark
0)
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值