資料結構實驗之排序七:選課名單 (SDUT 3404)
阿新 • • 發佈:2018-12-16
#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; }