數據結構實驗之排序七:選課名單
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
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct node
{
? ? char name[15];
? ? struct node *next;
}node;
node *nam[2010];
int num[2010];
int main()
{
? ? char str[15];
? ? int n,m,s,shu;
? ? int i;
? ? while(~scanf("%d%d",&n,&m))
? ? {
? ? ? ? memset(num,0,sizeof(num));
? ? ? ? for(i=0;i<2010;i++)
? ? ? ? {
? ? ? ? ? ? nam[i]=(struct node *)malloc(sizeof(struct node));
? ? ? ? ? ? nam[i]->next=NULL;
? ? ? ? }
? ? ? ? for(i=0;i<n;i++)
? ? ? ? {
? ? ? ? ? ? scanf("%s%d",str,&s);
? ? ? ? ? ? while(s--)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? scanf("%d",&shu);
? ? ? ? ? ? ? ? num[shu]++;
? ? ? ? ? ? ? ? node *q=(struct node *)malloc(sizeof(struct node));;
? ? ? ? ? ? ? ? q->next=NULL;
? ? ? ? ? ? ? ? strcpy(q->name,str);
? ? ? ? ? ? ? ? node *p=nam[shu];
? ? ? ? ? ? ? ? while(p->next)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if(strcmp(q->name,p->next->name)<0)
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? p=p->next;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? q->next=p->next;
? ? ? ? ? ? ? ? p->next=q;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? for(i=1;i<=m;i++)
? ? ? ? {
? ? ? ? ? ? printf("%d %d\n",i,num[i]);
? ? ? ? ? ? node *p=nam[i]->next;
? ? ? ? ? ? while(p)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? printf("%s\n",p->name);
? ? ? ? ? ? ? ? p=p->next;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? return 0;
}