杨辉三角如何实现

#ifndef _SEQUEUE_H_

#define _SEQUEUE_H_

#define MAXSIZE 1000

#define FAILURE 100001

#define SUCCESS 100002

typedef int ElemType;

struct queue

{

ElemType data[MAXSIZE];

int front;

int rear;

};

typedef struct queue SeQueue; 

int QueueInit(SeQueue *Q);

int EnQueue(SeQueue *Q,ElemType e);

int QueueLength(SeQueue Q);

ElemType GetFront(SeQueue Q);

ElemType DelQueue(SeQueue *Q);

#endif

include <stdio.h>
#include “SeQueue.h”

int QueueInit(SeQueue *Q)
{
Q->rear = Q->front = 0;
return SUCCESS;
}

int EnQueue(SeQueue *Q,ElemType e)
{
if((Q->rear +1) % MAXSIZE == Q->front)
{
return FAILURE;
}

Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1)% MAXSIZE;

return SUCCESS;
}

int QueueLength(SeQueue Q)
{
return ((Q.rear – Q.front + MAXSIZE) % MAXSIZE);
}

ElemType GetFront(SeQueue Q)
{
return Q.data[Q.front];
}

ElemType DelQueue(SeQueue *Q)
{
ElemType e;

if(Q->rear == Q->front)
{
return FAILURE;
}

e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;

return e;
}

#include <stdio.h>
#include “SeQueue.h”

int main()
{
int ret,i,j,t,length,num;
SeQueue sq,qs;

if(QueueInit(&sq) != SUCCESS || QueueInit(&qs) != SUCCESS)
{
printf(“Init Failure!!\n”);
}
printf(“Please input num\n”);
scanf(“%d”,&num);
for(i = 0; i < num;i++)
{
if(i == 0)
{
EnQueue(&sq,0);
EnQueue(&sq,1);
EnQueue(&sq,0);
printf(“1”);
}
else
{
EnQueue(&qs,0);
while(QueueLength(sq) != 1)
{
t=DelQueue(&sq)+GetFront(sq);
EnQueue(&qs,t);
}
DelQueue(&sq);
EnQueue(&qs,0);

length = QueueLength(qs);
for( j = 0; j < length; j++)
{
ret = DelQueue(&qs);
if(ret != 0)
{
printf(“%d “,ret);
}
EnQueue(&sq ,ret);
}

}
   printf(“\n”);
}

return 0;
}

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/mf1crystal/article/details/78709747
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞