栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言舞伴问题 PTA

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言舞伴问题 PTA

舞伴问题 PTA 描述

假设男士和女士的记录存放在一个数组中,设计算法实现舞伴配对,要求输出配对的舞伴,并输出没有配对的队头元素的姓名。

样例 输入样例

李敏浩 M
李钟硕 M
高欣雅 F
吴彦祖 M
王思聪 M
张甜源 F
张智霖 M
许丹丹 F
马小云 F

输出样例

高欣雅 李敏浩
张甜源 李钟硕
许丹丹 吴彦祖
马小云 王思聪

张智霖

函数定义接口:
void DancePartner(DataType dancer[], int num) ;

其中 dancer[]是存放男士和女士信息的数组,num是数组大小。

裁判测试程序样例:
#include
#include

typedef struct {
    char name[20]; 
    char sex; 
} DataType;

struct Node {
    DataType      data;
    struct Node*  next;
};
typedef struct Node  *PNode;
struct Queue
{
    PNode        f;
    PNode        r;
};
typedef struct Queue *linkQueue;
linkQueue  SetNullQueue_link()
{
    linkQueue lqueue;
    lqueue = (linkQueue)malloc(sizeof(struct Queue));
    if (lqueue != NULL)
    {
        lqueue->f = NULL;
        lqueue->r = NULL;
    }
    else
        printf("Alloc failure! n");
    return  lqueue;
}

int IsNullQueue_link(linkQueue lqueue)
{
    return (lqueue->f == NULL);
}

void EnQueue_link(linkQueue lqueue, DataType x)
{
    PNode  p;
    p = (PNode)malloc(sizeof(struct Node));
    if (p == NULL)
        printf("Alloc failure!");
    else {
        p->data = x;
        p->next = NULL;
        if (lqueue->f == NULL)
        {
            lqueue->f = p;
            lqueue->r = p;
        }
        else
        {
            lqueue->r->next = p;
            lqueue->r = p;
        }
    }
}
void DeQueue_link(linkQueue lqueue)
{
    struct Node  * p;
    if (lqueue->f == NULL)
        printf("It is empty queue!n ");
    else
    {
        p = lqueue->f;
        lqueue->f = lqueue->f->next;
        free(p);
    }
}
DataType  FrontQueue_link(linkQueue lqueue)
{
    if (lqueue->f == NULL)
    {
        printf("It is empty queue!n");
    }
    else
        return (lqueue->f->data);
}

void DancePartner(DataType dancer[], int num) 
{
            /* 请在这里填写答案 */
}

int main()
{
    DataType dancer[9];
    for (int i = 0; i < 9; i++)
    scanf("%s %c", dancer[i].name, &dancer[i].sex);
    DancePartner(dancer, 9);
    return 0;
}
思路一

只定义一个队列,如果配对队列出队,并且不进行入队操作。最后打印没有配对的队头元素的姓名。

void DancePartner(DataType dancer[], int num)
{
    linkQueue Queue_head = SetNullQueue_link();

    for (int i = 0; i < num; i++)
    {

        if (!IsNullQueue_link(Queue_head) &&
            (FrontQueue_link(Queue_head).sex) != dancer[i].sex)
        {
            if (dancer[i].sex == 'F')
                printf("%s %sn", dancer[i].name, Queue_head->f->data.name);
            else
            {
                printf("%s %sn", Queue_head->f->data.name, dancer[i].name);
            }
            DeQueue_link(Queue_head);
        }
        else
        {
            EnQueue_link(Queue_head, dancer[i]);
        }
    }
    printf("n");

    if (!IsNullQueue_link(Queue_head))
    {
        printf("%s", Queue_head->f->data.name);
    }
}

思路二

定义两个队列,先入队再一一匹配进行出队,最后打印没有配对的队头元素的姓名

void DancePartner(DataType dancer[], int num)
{
	linkQueue Mdancers = SetNullQueue_link();
	linkQueue Fdancers = SetNullQueue_link();
    for (int i = 0; i < num; ++i) {
        if (dancer[i].sex == 'M')
            EnQueue_link(Mdancers,dancer[i]);
        else{
            EnQueue_link(Fdancers,dancer[i]);
        }
        if (dancer[i].sex == 'M'){
            if (IsNullQueue_link(Fdancers) != 1){
                printf("%s %sn",FrontQueue_link(Fdancers).name,FrontQueue_link(Mdancers).name);
                DeQueue_link(Mdancers);
                DeQueue_link(Fdancers);
            }

        } else{
            if(IsNullQueue_link(Mdancers) != 1){
                printf("%s %sn",FrontQueue_link(Fdancers).name,FrontQueue_link(Mdancers).name);
                DeQueue_link(Mdancers);
                DeQueue_link(Fdancers);
            }
        }
    }
    printf("n");
    if (IsNullQueue_link(Mdancers) != 1)
        printf("%s",FrontQueue_link(Mdancers).name);
    if (IsNullQueue_link(Fdancers) != 1)
        printf("%s",FrontQueue_link(Fdancers).name);
}
END

相信你们刚看队列的时候,跟昨天的我一样也是闷逼的。

    lqueue->f = NULL;
    lqueue->r = NULL;

队列中的lqueue->f指向的队头(出队的地方),lqueue->r指向队尾(入队的地方)。

随着对上述题目的不断阅读与理解,我发现队列有更复杂的结构。以前的学的单链表,双链表只是为了服务于后面的队列,栈,树这类。如果你认真看过题目的代码,再看下面这个语句应该就懂了这种套娃。
Q u e u e _ h e a d ⟶ f ⟶ d a t a . n a m e Queue_headlongrightarrow{}flongrightarrow{}data.name Queue_head⟶f⟶data.name
对上面式子进行化简
a = Q u e u e _ h e a d ⟶ f a ⟶ d a t a . n a m e a=Queue_headlongrightarrow{}f\ alongrightarrow{}data.name a=Queue_head⟶fa⟶data.name
a ⟶ d a t a . n a m e alongrightarrow{}data.name a⟶data.name这个是不是挺眼熟的

struct Node {
    DataType      data;
    struct Node*  next;
};		

一看,诶还有next,这不就是链表嘛。



对于.data.name有些人也会感到疑惑,这个是怎么来的。这个是结构体的用法,平时用的箭头是访问结构体指针中的变量。忘记的小伙伴再百度百度相关的知识点吧。

data用DataType去定义,而DataType用如下的方式进行typedef

typedef struct {
    char name[20]; 
    char sex; 
} DataType;

以前只知道typedef有用,但是不太了解,经历了这题我算是懂了。自己去创造一个数值类型的真的牛批,在大项目中还可以一键修改,有一点宏定义的感觉。

唔,这里还用到了结构体数组。

DataType dancer[9];

就不详细写了,戛然而止式结尾。

ps:
如果有些同学是部分正确但是始终找不错误:
对于不匹配的同学,只输出一个同学的名字,不是把所有不匹配的名字都打印。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/290460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号