just a learner 2023-10-31 10:28 采纳率: 0%
浏览 36

数据结构中的舞伴问题

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

  • 写回答

2条回答 默认 最新

  • 就不设置昵称了吧 2023-10-31 12:52
    关注

    我的思路是使用队列这种数据结构,先入先出,每个人跳完舞之后再次入队,等待下一轮舞曲。
    使用链表构造的一个队列,代码如下,代码哪里不对或者不好,请告诉我。

    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define QUEUE_MAX_LENGHT 20
    
    struct Element
    {
        char name[20];
        struct Element *pre;
        struct Element *next;
    };
    
    struct Queue
    {
        int length;
        struct Element *head;
        struct Element *tail;
    };
    
    // 出队
    struct Element *take(struct Queue *queue)
    {
        if (queue->length < 1)
        {
            printf("Queue is empty.");
            exit(1);
        }
        struct Element *old_head = queue->head;
    
        struct Element *new_head = old_head->next;
        queue->head = new_head;
        queue->length--;
    
        old_head->next = NULL;
        old_head->pre = NULL;
        return old_head;
    }
    
    // 入队
    void push(struct Queue *queue, struct Element *element)
    {
        if (queue->length >= 20)
        {
            printf("Queue too long.");
            exit(1);
        }
    
        if (queue->tail == NULL)
        {
            queue->head = element;
            queue->tail = element;
            queue->length++;
        }
        else
        {
            struct Element *old_tail = queue->tail;
            old_tail->next = element;
            element->pre = old_tail;
    
            queue->tail = element;
            queue->length++;
        }
    }
    
    struct Queue *init_queue()
    {
        struct Queue *queue = (struct Queue *)malloc(sizeof(struct Queue));
        queue->length = 0;
        queue->head = NULL;
        queue->tail = NULL;
        return queue;
    }
    
    struct Element *init_element(char *name)
    {
        struct Element *element = (struct Element *)malloc(sizeof(struct Element));
        strcpy(element->name, name);
        element->pre = NULL;
        element->next = NULL;
        return element;
    }
    
    int main()
    {
        struct Queue *man_queue = init_queue();
        struct Queue *miss_queue = init_queue();
    
        int i;
        // 初始化男士队列
        for (i = 0; i < 10; i++)
        {
            char name[20] = "man ";
    
            char num[10];
            sprintf(num, "%d", i);
    
            strcat(name, num);
    
            struct Element *new_man = init_element(name);
            push(man_queue, new_man);
        }
    
        // 初始化女士队列
        for (i = 0; i < 8; i++)
        {
            char name[20] = "miss ";
    
            char num[10];
            sprintf(num, "%d", i);
    
            strcat(name, num);
    
            struct Element *new_miss = init_element(name);
            push(miss_queue, new_miss);
        }
    
        int min_num;
        min_num = (miss_queue->length > man_queue->length) ? man_queue->length : miss_queue->length; // 取短队伍的数量。
    
        int j;
        for (i = 0; i < 3; i++) // 三支舞曲
        {
            printf("dance music %d\n", i + 1);
    
            for (j = 0; j < min_num; j++) // 短队伍的人都要匹配到。
            {
                struct Element *miss = take(miss_queue); // 取出队列中排队的第一人。
                struct Element *man = take(man_queue);
                printf("dance partners is '%s' and '%s'\n", miss->name, man->name);
                // 跳舞中
                push(miss_queue, miss); // 跳完舞之后再次入队,等待下一首舞曲。
                push(man_queue, man);
            }
        }
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 10月31日