#include
#include
#define OK 1
#define ERROR -1
#define OVERFLOW -2
#define MAXQSIZE 100
typedef int Status;
typedef struct{
int *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q)
{
Q.base=(int *)malloc(MAXQSIZE*sizeof(int));
if(!Q.base) exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
Status EnQueue(SqQueue &Q,int e)
{
if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
Status DeQueue(SqQueue &Q,int &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
}
Status GetHead(SqQueue Q,int &e)
{
e=Q.base[Q.front];
return OK;
}
void Print(int n)
{
SqQueue Q;
int i,s,e;
InitQueue(Q);
EnQueue(Q,0);
EnQueue(Q,1);
EnQueue(Q,0);
for(i=1;i<=n;i++)
{
do
{
DeQueue(Q,s);
GetHead(Q,e);
if (e!=0)
printf ("%d ", e);
EnQueue(Q,s+e);
}while(e!=0);
printf("\n");
EnQueue(Q,0);
}
}
int main()
{
int n;
scanf("%d",&n);
Print(n);
return 0;
}
#include
void main()
{
int i,j,n=13;
printf("N=");
while(n>12)
scanf("%d",&n); /*控制输入正确的值以保证屏幕显示的图形正确*/
for(i=0;i<=n;i++) /*控制输出N行*/
{
for(j=0;j<24-2*i;j++) printf(" "); /*控制输出第i行前面的空格*/
for(j=1;j<i+2;j++) printf("%4d",c(i,j)); /*输出第i行的第j个值*/
printf("\n");
}
}
int c(int x,int y) /*求杨辉三角形中第x行第y列的值*/
{
int z;
if((y==1)||(y==x+1)) return 1; /*若为x行的第1或第x+1列,则输出1*/
z=c(x-1,y-1)+c(x-1,y); /*否则,其值为前一行中第y-1列与第y列值之和*/
return z;
}
你的有什么问题么?我看对着呢啊
杨辉三角(Pascal Triangle)的几种C语言实现及其复杂度分析
就你贴出的程序,是使用队列的方式来求解杨辉三角形的思路。
上面的答案有帮助吗?如果还有问题,请提出来,如果对答案满意,请顶一下,并标记为采纳答案,谢谢!