前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据结构实验之排序七:选课名单 (SDUT 3404)

数据结构实验之排序七:选课名单 (SDUT 3404)

作者头像
Lokinli
发布2023-03-09 18:48:26
1520
发布2023-03-09 18:48:26
举报
文章被收录于专栏:以终为始以终为始
代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node
{
    char data[15];
    struct node *next;  //存放名字
};

struct node *head[2018];  // 每个课程都有一个相应的开始
int num[2018];  // 存放每个课程选的人数
char name[15];

int main()
{

    int n,m,sum,cnt;
    while(~scanf("%d%d",&n,&m))
    {
        memset(num,0,sizeof(num));
        for(int i = 0; i < 2018; i ++)  // 初始化
        {
            head[i] = (struct node *)malloc(sizeof(struct node));
            head[i] -> next = NULL;
        }
        for(int i = 0; i < n; i ++)  
        {
            scanf("%s %d", name, &sum); 
            for(int j = 0; j < sum; j ++)
            {
                scanf("%d", &cnt);  
                num[cnt] ++;  // 对应的课程的人数要+1
                struct node *q, *p;
                q = (struct node *)malloc(sizeof(struct node));
                q -> next = NULL;  
                strcpy(q -> data,name); // 新申请一个结点来存放这个人的名字
                p = head[cnt]; // 找到这个人应该的在的那一列中
                while(p -> next)
                {
                    if(strcmp(q -> data, p -> next -> data) < 0) // 找到第一个字符串需要大于它的
                    {
                        break;
                    }
                    p = p -> next;
                }
                q -> next = p -> next;  // 顺序插入到链表中去
                p -> next = q;
            }
        }
        for(int i = 1; i <= m; i ++)
        {
            printf("%d %d\n", i, num[i]); // 输出课程 以及人数
            struct node *p; // 对应的人名
            p = head[i] -> next;
            while(p)
            {
                printf("%s\n", p -> data);
                p = p -> next;
            }
        }

    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档