舞伴问题的设计与实现

1.问题描述

假设在周末舞会上,男士们和女士们在进入舞厅时,各自排成一队。跳舞开始时,依次从男队和女队的队头各出一人配成舞伴。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。一支舞曲结束所有刚跳过舞的人按原来的顺序排到原队列的后面。请输出前三支舞曲的男女配对名单。

2.一个完整的系统应具有以下功能:

1) 数据结构的定义和初始化,舞会开始时各数据结构的状态;

2) 根据跳舞者的性别依次将其插入相应队列;

3) 依次输出配成的男女舞伴的姓名;

4) 若某队仍有等待配对者,则等待下一个舞曲优先配对跳舞,一支舞曲结束所有刚跳过舞的人按原来的顺序排到原队列的后面。

实验目的:

1)通过实验掌握对离散事件模拟的认识;

2)进一步理解队列的实现与应用;

3)该实验涉及到数组的建立操作,涉及到了队列的建立、插入、删除,涉及到了离散事件的应用思想。完成这个实验对线性表、队列及C语言编程等多方面的知识将是一个很好的利用,对离散事件也将有一个初步的认识。

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>

#define MAX_SIZE 100

// 结构体定义:用于表示舞者
typedef struct {
    char name[20];
    char gender; // 'M'代表男性,'F'代表女性
} Dancer;

// 队列结构体定义
typedef struct {
    Dancer dancers[MAX_SIZE];
    int front, rear;
} Queue;

// 初始化队列
void initializeQueue(Queue *q) {
    q->front = q->rear = -1;
}

// 判断队列是否为空
int isEmpty(Queue *q) {
    return q->front == -1;
}

// 插入元素到队列
void enqueue(Queue *q, Dancer dancer) {
    if (q->rear == MAX_SIZE - 1) {
        printf("队列已满,无法插入。\n");
    } else {
        if (isEmpty(q)) {
            q->front = q->rear = 0;
        } else {
            q->rear++;
        }
        q->dancers[q->rear] = dancer;
    }
}

// 删除队列的第一个元素
Dancer dequeue(Queue *q) {
    Dancer dancer;
    if (isEmpty(q)) {
        printf("队列为空,无法删除。\n");
        strcpy(dancer.name, "NULL");
        dancer.gender = 'N';
    } else {
        dancer = q->dancers[q->front];
        if (q->front == q->rear) {
            q->front = q->rear = -1;
        } else {
            q->front++;
        }
    }
    return dancer;
}

int main() {
    Queue maleQueue, femaleQueue;
    initializeQueue(&maleQueue);
    initializeQueue(&femaleQueue);

    Dancer maleDancers[MAX_SIZE], femaleDancers[MAX_SIZE];

    // 假设有一些初始的男性和女性舞者
    // 你可以在这里初始化他们的信息

    // 模拟前三支舞曲的情况
    for (int danceRound = 1; danceRound <= 3; danceRound++) {
        printf("第 %d 支舞曲的舞伴情况:\n", danceRound);
        
        while (!isEmpty(&maleQueue) && !isEmpty(&femaleQueue)) {
            Dancer male = dequeue(&maleQueue);
            Dancer female = dequeue(&femaleQueue);
            
            printf("%s 和 %s\n", male.name, female.name);
            
            // 将刚跳过舞的人重新插入队列的尾部
            enqueue(&maleQueue, male);
            enqueue(&femaleQueue, female);
        }
    }
    
    return 0;
}

在上面这串代码中插入可供输入男女数量的代码

#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
#define OVERFLOW 0

typedef int Status;
 
/*跳舞这个人信息*/
typedef struct
{
    char name[20];//名字
    char sex;//F为女性,M为男性
}Person;
 
/*队列的顺序存储结构*/
typedef struct
{
    Person* base;//队列中数据元素类型为Person
    int front;//头指针
    int rear;//尾指针
}SqQueue;
 
typedef Person ElemType;
 
/*队列初始化*/
Status InitQueue(SqQueue& Q)
{//构造一个空队列Q
    Q.base = new Person[MAXSIZE];
    if (!Q.base)
        exit(OVERFLOW);
    Q.front = Q.rear = 0;
    return OK;
}
 
/*求队列的长度*/
int QueueLength(SqQueue* Q)
{
    return(Q->rear - Q->front + MAXSIZE) % MAXSIZE;
}
 
/*判断队列是否为空*/
bool QueueEmpty(SqQueue& Q)
{
    if (Q.front == Q.rear)
        return true;//队满
    else
        return false;
}
 
Status EnQueue(SqQueue& Q, Person e)
{//插入元素e为Q的新的队尾元素
    if ((Q.rear + 1) % MAXSIZE == Q.front)//尾指针在循环意义上加1后等于头指针,表明队满
    {
        return ERROR;
    }
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear + 1) % MAXSIZE;//队尾指针加1
    return OK;
}
 
/*出队(保留队头元素)删除Q的队头元素,用e返回其值*/
Status DeQueue(SqQueue& Q, Person& e)
{
    if (Q.front == Q.rear)
        return ERROR;//队空
    e = Q.base[Q.front];  //保留队头元素
    Q.front = (Q.front + 1) % MAXSIZE;//队头指针加1
    return OK;
}
 
/*出队(不保留对头元素)*/
Status DeQueuee(SqQueue& Q)
{
    if (Q.front == Q.rear)
        return ERROR;
    Q.front = (Q.front + 1) % MAXSIZE;
    return OK;
}
 
//取队头元素
Person GetHead(SqQueue& Q)
{//返回Q的队头元素,不修改队头指针
    if (Q.rear != Q.front)
    {
        return Q.base[Q.front];//返回队头元素的值,队头指针不变
    }//队列非空
}
 
//舞伴问题解决
void dancersolve(Person dancer[], int num, int m, SqQueue male, SqQueue female)
{//结构体数组中dancer中存放跳舞的男女姓名,num是跳舞的人数
    for (int j = 0; j < m; j++)
    {
        cout << "第" << j + 1 << "轮:" << endl;
        for (int i = 0; i < num; i++)
        {
            if (dancer[i].sex == 'F')
                EnQueue(female, dancer[i]);//插入女队
            else
                EnQueue(male, dancer[i]);//插入男队
        }
        cout << "The dancing partners are:\n";
        while (!QueueEmpty(male) && !QueueEmpty(female))
        {//依次输入男女舞伴问题
            Person p;
            DeQueue(male, p);
            cout << p.name << ' ';//女士出列
            DeQueue(female, p);
            cout << p.name << endl;//男士出列
        }
        Person e;
        if (!QueueEmpty(female))//女士队列非空,输出队头女士的姓名
        {
            e = GetHead(female);//取女士队头
            cout << "The first woman to get a partner is:" << e.name << endl;
        }
        else if (!QueueEmpty(male))//男士队列非空,输出队头男士的姓名
        {
            e = GetHead(female);//取男士队头
            cout << "The first man to get a partner is:" << e.name << endl;
        }
    }
}
 
//主函数
int main()
{
    int n;
    cout << "请输入跳舞人的人数:";
    cin >> n;
    Person* dancer = new Person[n];
    cout << "请依次输入跳舞人的信息:" << endl;
    for (int i = 0; i < n; i++)
    {
        cout << "name:";
        cin >> dancer[i].name;
        cout << "sex:";
        cin >> dancer[i].sex;
    }
    int m;
    cout << "请输入舞会的轮数:";
    cin >> m;
 
    SqQueue male, female;
    InitQueue(male);
    InitQueue(female);
 
    dancersolve(dancer, n, m, male, female);
    return 0;
}

这个修改后的代码允许用户输入男性和女性舞者的数量以及他们的姓名,并在前三支舞曲中模拟舞伴的配对。请注意,这只是一个基本示例,你可以根据实际需要进一步扩展和改进。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值