数据结构_队列-循环队列实现模拟舞伴配对问题

循环队列的应用——舞伴配对问题:在舞会上,男、女各自排成一队。舞会开始时,依次从男队和女队的队头各出一人配成舞伴。如果两队初始人数不等,则较长的那一队中未配对者等待下一轮舞曲。假设初始男、女人数及性别已经固定,舞会的轮数从键盘输入。试模拟解决上述舞伴配对问题。要求:从屏幕输出每一轮舞伴配对名单,如果在该轮有未配对的,能够从屏幕显示下一轮第一个出场的未配对者的姓名。

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. typedef struct Queue{  
  5.     int Front;  
  6.     int Rear;  
  7.     char elem[100][100];  
  8.     int Queuesize;  
  9. }Queue;  
  10.   
  11. void Creat_Queue(Queue &Q)  
  12. {//建立一个队列  
  13.     int n,i;  
  14.     Q.Front=Q.Rear=0;  
  15.     printf("请输入跳舞人数:");  
  16.     scanf("%d",&n);  
  17.     Q.Queuesize=n+1;  
  18.     printf("请输入各跳舞人名:");  
  19.     for(i=0;i<n;i++)  
  20.         scanf("%s",&Q.elem[i]);  
  21.     Q.Rear=n;  
  22. }  
  23.   
  24. int QueueEmpty(Queue Q)  
  25. {//判断队列是否为空  
  26.     if(Q.Front==Q.Rear)  
  27.         return 1;  
  28.     else  
  29.         return 0;  
  30. }  
  31. void DeQueue(Queue &Q,char *str)  
  32. {//删除队头元素  
  33.     strcpy(str,Q.elem[Q.Front]);  
  34.     Q.Front=(Q.Front+1)%Q.Queuesize;  
  35. }  
  36. void GetQueue(Queue Q,char *str)  
  37. {//取队首元素,队头指针不改变  
  38.     strcpy(str,Q.elem[Q.Front]);  
  39. }  
  40.   
  41. void Judge_Queue(Queue &M,Queue &W)  
  42. {//舞伴配对  
  43.     int n;  
  44.     char str1[100],str2[100];  
  45.     printf("请输入舞会的轮数:");  
  46.     scanf("%d",&n);  
  47.     while(n--)  
  48.     {  
  49.         while(!QueueEmpty(M))  
  50.         {  
  51.             if(QueueEmpty(W))  
  52.                 DeQueue(W,str1);  
  53.             DeQueue(M,str1);  
  54.             DeQueue(W,str2);  
  55.             printf("配对的舞者:%s %s\n",str1,str2);  
  56.         }  
  57.         M.Front=(M.Front+1)%M.Queuesize;  
  58.         if(QueueEmpty(W))  
  59.                 DeQueue(W,str1);  
  60.         GetQueue(W,str1);  
  61.         printf("第一个出场的未配对者的姓名:%s\n",str1);  
  62.     }  
  63. }  
  64.   
  65. int main()  
  66. {  
  67.     Queue M,W;  
  68.     printf("男队:\n");  
  69.     Creat_Queue(M);  
  70.     printf("女队:\n");  
  71.     Creat_Queue(W);  
  72.     if(M.Queuesize>W.Queuesize)  
  73.         Judge_Queue(W,M);  
  74.     else  
  75.         Judge_Queue(M,W);  
  76.   
  77.     return 0;  
  78. }  
#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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值