#include<stdio.h>
#define maxsize 100
typedef struct node
{
char sex;
int num;
} person;
typedef struct Node
{
int data[maxsize];
int front;
int rear;
} dancers;
void initqueue(dancers *q)
{
q->front=q->rear=0;
}
int inqueue(dancers *q,int x)
{
if(q->front==(q->rear+1)%maxsize)
return 0;
else
{
q->data[q->rear]=x;
q->rear=(q->rear+1)%maxsize;
return 1;
}
}
int outqueue(dancers *q,int *x)
{
if(q->front==q->rear)
return 0;
else
{
*x=q->data[q->front];
q->front=(q->front+1)%maxsize;
return 1;
}
}
int main()
{
person c;
dancers r,p;
initqueue(&r);
initqueue(&p);
int n,l,m,flag=0;
scanf("%d",&n);
int i,j;
for(i=1;i<=n;i++)
{
getchar();
scanf("%c %d",&c.sex,&c.num);
if(c.sex=='w')
{
inqueue(&r,c.num);
}
else
inqueue(&p,c.num);
}
printf("配对成功的舞伴有:");
while(r.front!=r.rear&&p.front!=p.rear)
{
outqueue(&r,&m);
outqueue(&p,&l);
printf("%d和%d\n",m,l);
}
printf("等待下一曲舞的是:");
while(outqueue(&r,&m))
{
flag=1;
printf("%d ",m);
}
while(outqueue(&p,&m))
{
flag=1;
printf("%d ",m);
}
if(flag==0)
printf("0\n");
return 0;
}
舞伴配对问题
最新推荐文章于 2021-08-27 10:12:36 发布