当前位置: 代码迷 >> 综合 >> 数据结构实验之排序七:选课名单 oj
  详细解决方案

数据结构实验之排序七:选课名单 oj

热度:23   发布时间:2024-01-14 22:46:40.0

数据结构实验之排序七:选课名单

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

随着学校规模的扩大,学生人数急剧增加,选课名单的输出也成为一个繁重的任务,我校目前有在校生3万多名,两千多门课程,请根据给定的学生选课清单输出每门课的选课学生名单。

Input

输入第一行给出两个正整数N( N ≤ 35000)和M(M ≤ 2000),其中N是全校学生总数,M是课程总数,随后给出N行,每行包括学生姓名拼音+学号后两位(字符串总长度小于10)、数字S代表该学生选课的总数,随后是S个课程编号,约定课程编号从1到M,数据之间以空格分隔。

Output

按课程编号递增的顺序输出课程编号、选课总人数以及选课学生名单,对选修同一门课程的学生按姓名的字典序输出学生名单。数据之间以空格分隔,行末不得有多余空格。

Example Input
5 3
Jack01 2 2 3
Jone01 2 1 3
Anni02 1 1
Harry01 2 1 3
TBH27 1 1
Example Output
1 4
Anni02
Harry01
Jone01
TBH27
2 1
Jack01
3 3
Harry01
Jack01
Jone01
Hint
Author
xam
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
    char name[12];
    struct node *next;
}*head[2002];
int a[2002];
int main()
{
    int n,i,j,m,num,sum;
    char str[12];
    struct node *tail,*q,*p;
    scanf("%d%d",&n,&m);
    for(i = 1;i <= m;i++)
    {
        head[i] = (struct node*)malloc(sizeof(struct node));
        head[i]->next = NULL;
        a[i] = 0;
    }
    for(i = 0;i < n;i++)
    {
        scanf("%s%d",str,&num);
        for(j = 0;j < num;j++)
        {
            p = (struct node *)malloc(sizeof(struct node));
            strcpy(p->name,str);
            scanf("%d",&sum);
            tail = head[sum]->next;
            q = head[sum];
            a[sum]++;
            while(tail != NULL)
            {
                if(strcmp(p->name,tail->name) < 0)
                {
                    q->next = p;
                    p->next = tail;
                    break;
                }
                q = tail;
                tail = tail->next;
            }
            if(tail == NULL)
            {
                p->next = NULL;
                q->next = p;
            }
        }
    }
    for(i = 1;i <= m;i++)
    {
        printf("%d %d\n",i,a[i]);
        for(p = head[i]->next;p!=NULL;p=p->next)
        {
            printf("%s\n",p->name);
        }
    }
    return 0;
}