您的位置:首页 > 理论基础 > 数据结构算法

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

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

[cpp]
view plain
copy

print?

#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;  
}  



#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
4000
.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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐