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

版权声明:欢迎转载,注明出处QWQ https://blog.csdn.net/Mercury_Lc/article/details/84978375
#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;
}

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/84978375