杨辉三角

来源:互联网 发布:淘宝文案策划怎么写 编辑:程序博客网 时间:2024/04/28 22:26

#include<iostream.h>
#include<stdlib.h>
#include <iomanip.h>

#define max 50
typedef struct
{
 int a[max];
 int front;
 int rear;
}sqlqueue;
void init(sqlqueue **q)
{
 *q=(sqlqueue *)malloc(sizeof(sqlqueue));
 (*q)->front=(*q)->rear=0;
}
void  enter(sqlqueue *q,int x)
{
 if(q->rear+1==q->front)
 {
  cout<<"队列已满"<<endl;
       
 }
 q->a[q->rear]=x;
 q->rear=(q->rear+1)%max;
 
 
}

void out(sqlqueue *q,int &x)
{
 if(q->front==q->rear)cout<<"队列一空"<<endl;
 x=q->a[q->front];
 q->front=(q->front+1)%max;
}
void yanghui(sqlqueue *q)
{
  
 int x;
 enter(q,1);
 enter(q,2);
 enter(q,1);
 
 for(int i=0;i<9;i++)
 {
  for(int j=0;j<9-i;j++)
   cout<<setw(2)<<" ";
  if(i==0)
   cout<<setw(2)<<1<<endl;
  else
  {if(i==1)
  cout<<setw(2)<<1<<setw(2)<<" "<<setw(2)<<1<<endl;
  else
   
   
   while(1)
   {
    
    if(q->a[q->front]==1&&q->a[q->front+1]==1)
    {
     enter(q,1);
     out(q,x);
     cout<<setw(2)<<x<<setw(2)<<" "<<endl;
     break;
    }
    else
    {if(q->a[q->front]==1&&q->a[q->front+1]!=1)
    {
     enter(q,1);
     enter(q,q->a[q->front]+q->a[q->front+1]);
     out(q,x);
    
     cout<<setw(2)<<x<<setw(2)<<" ";
     
    }
    else
    {
     int t=q->a[q->front]+q->a[q->front+1];
     enter(q,t);
     out(q,x);
     
     cout<<setw(2)<<x<<setw(2)<<" ";
     
    }
    }
    
    
    
   }
  }
  
  
 }

}

void main()
{
 sqlqueue  *q;
 init(&q);
 yanghui(q);
 //q=(sqlqueue *)malloc(sizeof(sqlqueue));

}