利用循环队列模拟舞伴配对问题

利用循环队列模拟舞伴配对问题:

1、利用循环队列模拟舞伴配对问题。在舞会上,男、女各自排成一队。舞会开始时。依次从男队和女队的队头各出一人配成舞伴。如果两队初始人数不等,则较长的那一队中未配对者等待下一轮舞曲。

2、假设初始男、女人数及性别已经固定,舞会的轮数从键盘输入。

试模拟解决上述舞伴配对问题。

3、从屏幕输出每一轮舞伴配对名单,如果在该轮有未配对的,能够从屏幕显示下一轮第一个出场的未配对者的姓名。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Queue{
    int Front;
    int Rear;
    char elem[100][100];
    int Queuesize;
}Queue;

void Creat_Queue(Queue &Q)
{//建立一个队列
    int n,i;
    Q.Front=Q.Rear=0;
    printf("请输入跳舞人数:");
    scanf("%d",&n);
    Q.Queuesize=n+1;
    printf("请输入各跳舞人名:");
    for(i=0;i<n;i++)
        scanf("%s",&Q.elem[i]);
    Q.Rear=n;
}

int QueueEmpty(Queue Q)
{//判断队列是否为空
    if(Q.Front==Q.Rear)
        return 1;
    else
        return 0;
}
void DeQueue(Queue &Q,char *str)
{//删除队头元素
    strcpy(str,Q.elem[Q.Front]);
    Q.Front=(Q.Front+1)%Q.Queuesize;
}
void GetQueue(Queue Q,char *str)
{//取队首元素,队头指针不改变
    strcpy(str,Q.elem[Q.Front]);
}

void Judge_Queue(Queue &M,Queue &W)
{//舞伴配对
    int n;
    char str1[100],str2[100];
    printf("请输入舞会的轮数:");
    scanf("%d",&n);
    while(n--)
    {
        while(!QueueEmpty(M))
        {
            if(QueueEmpty(W))
                DeQueue(W,str1);
            DeQueue(M,str1);
            DeQueue(W,str2);
            printf("配对的舞者:%s %s\n",str1,str2);
        }
        M.Front=(M.Front+1)%M.Queuesize;
        if(QueueEmpty(W))
                DeQueue(W,str1);
        GetQueue(W,str1);
        printf("第一个出场的未配对者的姓名:%s\n",str1);
    }
}

int main()
{
    Queue M,W;
    printf("男队:\n");
    Creat_Queue(M);
    printf("女队:\n");
    Creat_Queue(W);
    if(M.Queuesize>W.Queuesize)
        Judge_Queue(W,M);
    else
        Judge_Queue(M,W);

    return 0;
}

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值