1 1 0(用0作为行打印的边界条件)tmp作为临时变量,初始化等于0
1 2 1 0 (tmp(0)+1,tmp(1)+1,tmp(1)+0) 规律
1 3 3 1 0 (tmp(0)+1,tmp(1)+2,tmp(2)+1,tmp(1)+0) tmp的顺序输入值是上一行队列里的反向出队列的值,被加数是上一行队列的顺序输出值。代码如下:
#include<iostream>
using namespace std;
#define NUM 50
template <class T>
class Seq
{
private:
T * elem;
int front;
int rear;
int maxsize;
int size;
public:
void create();
void enQue(T x);
void deQue();
T getHead();
bool isEmpty();
void traverse();
};
template <class T>
void Seq<T>::create()
{
elem=new T[NUM];
front=0;
rear=0;
maxsize=NUM;
size=0;
}
template <class T>
void Seq<T>::enQue(T x)
{
if((rear+1)%maxsize==front) cout<<"The queue is full !";
else {rear=(rear+1)%maxsize; elem[rear]=x; size++; }
}
template <class T>
void Seq<T>::deQue()
{
if(rear==front) cout<<"There is no data !";
else {front=(front+1)%maxsize; size--; }
}
template <class T>
T Seq<T>::getHead()
{
return elem[(front+1)%maxsize];
}
template <class T>
bool Seq<T>::isEmpty()
{
if(rear==front) return true;
else return false;
}
template <class T>
void Seq<T>::traverse()
{
for(int i=0;i<size;i++)
{
cout<<elem[(front+1+i)%maxsize]<<" ";
}
}
void yanghui(int n)
{
Seq<int> Q;
int tmp=0;
int k=1;
cout<<1<<endl;;
Q.create();
Q.enQue(1);
Q.enQue(1);
Q.enQue(0);
while(k<n)
{
while(Q.getHead()!=0)
{
Q.enQue(tmp+Q.getHead());
tmp=Q.getHead();
cout<<Q.getHead();
Q.deQue();
}
k++;
tmp+=Q.getHead();
Q.enQue(tmp);
Q.enQue(0);
Q.deQue();
tmp=0;
cout<<endl;
}
}
int main()
{
yanghui(5);
}