数据结构_舞伴问题(队列)

舞伴问题
假设在周末舞会上,男士们和女士们进入舞厅时,各自排成一队,男女人数不一定相等。

跳舞开始时,依次从男队和女队的 队首上各出一人配成舞伴。 舞曲结束后,男女各自入队。
现在用小写字母表示男士,大写字母表示女士,
试输入一个字符串,按照大小写将男女归入两个队列,
试写出当所有人都至少跳舞一次时,舞伴的组合情况。
测试样例格式说明:
[键盘输入]
用字符串表示的当天舞者序列;
[正确输出]
舞伴组合情况

测试样例:
[第一组自测数据]
[键盘输入]
abcDefGiH
[正确输出]
a - D
b - G
c - H
e - D
f - G
i - H

[第二组自测数据]
[键盘输入]
aBcDEF
[正确输出]
a - B
c - D
a - E
c - F
*/

/*int 类型数组也可以存储读入的字符char类型,也可将int修改为char*/
#define MAXLEN 100
#define EMPTY_QUEUE_ERROR -9999999
typedef int ElemType;
typedef struct {
int elem[MAXLEN]; // 用来存队列数据元素的数组
int front; // 指示队首在数组中位置的标识量
int rear; // 指示队尾在数组中位置的标识量adj.后面的,背面的,后方的
}intQueue;
/*初始化(调整两个位置标识量)*/
void InitQueue(intQueue* pQueue) // 指针类型作为形参,某个队列实例的地址
{
/**********************************************************
将该队列实例的队首和队尾标识量置零
*********************************************************/
pQueue->front = 0;
pQueue->rear = 0;
}
/*对头元素出队列*/
int DeQueue(intQueue* pQueue)
{
if (!IsEmpty(pQueue))
{
/*移动对头元素前保存该值*/
ElemType temp = pQueue->elem[pQueue->front];
/*向前移动front有讲究:
如果是:pQueue->front++,那么front可能会溢出数组,
所以必须利用循环队列的周期性质(除以周期取余数),保证不溢出*/
pQueue->front = (pQueue->front + 1) % MAXLEN;
return temp;/*删除成功的出口*/
}
return EMPTY_QUEUE_ERROR;
}
/*入队列
将某数据元素x入队列,成功返回1,失败返回0。*/
int EnQueue(intQueue* pQueue, ElemType x)
{
// 1、若该队列已满,则入队列失败
if (IsFull(pQueue)) return 0;
// 2、若该队列没满,将x写入队尾,然后队尾位置后移
else
{
pQueue->elem[pQueue->rear] = x;// x入队列
pQueue->rear = (pQueue->rear + 1) % MAXLEN;// 队尾后移
}
return 1;
}
/*
(9)求取队列长度(重要)
返回队列中元素的个数
*/
int QueueLen(intQueue* q)
{
return (q->rear - q->front + MAXLEN) % MAXLEN;
}
void dance_match()
{
intQueue qMale ,
qFemale;/*定义两个队列*/
char c;
int LenMale,
LenFemale;/*记录男女队列是否分别完成遍历一次*/
char backMale, backFemale;/*记录要重新入队的元素*/
/*初始化定义的队列*/
InitQueue(&qMale);
InitQueue(&qFemale);
/*读取输入并填充队列*/
printf("读取输入(字符串)并填充队列:\n其中,女士用大写字母,男士用小写字母表示:\n");
while (scanf("%c", &c) && c != '\n')
{
/*女士用大写:*/
if (c >= 'A' && c <= 'Z')
{
EnQueue(&qFemale, c);
}
else if(c >= 'a' && c <= 'z')
{
EnQueue(&qMale, c);
}
}
LenMale = QueueLen(&qMale);
LenFemale = QueueLen(&qFemale);
printf("输出配对结果:\n");
while (LenFemale || LenMale)//若双方均为0,就不再进入循环了
{
/*LenMale与LenFemale双方均非0*/
if (LenFemale && LenMale)
{
/*配对流程*/
printf("%c - %c\n", backMale = DeQueue(&qMale), backFemale = DeQueue(&qFemale));
EnQueue(&qMale, backMale);
EnQueue(&qFemale, backFemale);
/*分类减少*/
LenFemale--;
LenMale--;
}//if
/*LenMale与LenFemale一方为0,另一方非0*/
else if (!LenMale && LenFemale)/*如果男士先完成遍历*/
{
/*配对流程*/
printf("%c - %c\n", backMale = DeQueue(&qMale), backFemale = DeQueue(&qFemale));
EnQueue(&qMale, backMale);
EnQueue(&qFemale, backFemale);
LenFemale--;
}
else
{
/*配对流程*/
printf("%c - %c\n", backMale = DeQueue(&qMale), backFemale = DeQueue(&qFemale));
EnQueue(&qMale, backMale);
EnQueue(&qFemale, backFemale);
LenMale--;
}
}//while
}
int main()
{
dance_match();
}


在这里插入图片描述

posted @   xuchaoxin1375  阅读(55)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2023-09-05 CN_@传输层协议@端口
2023-09-05 CN_UDP协议
2023-09-05 输入int a,b;求最简分数,
点击右上角即可分享
微信分享提示