数据结构课设 打印选课学生名单 。。

来源:互联网 发布:shake it off 网盘 编辑:程序博客网 时间:2024/05/22 17:35

5-14 打印选课学生名单   (25分)

假设全校有最多40000名学生和最多2500门课程。现给出每个学生的选课清单,要求输出每门课的选课学生名单。

输入格式:

输入的第一行是两个正整数:N(\le40000),为全校学生总数;K(\le2500),为总课程数。此后N行,每行包括一个学生姓名(3个大写英文字母+1位数字)、一个正整数C(\le20)代表该生所选的课程门数、随后是C个课程编号。简单起见,课程从1到K编号。

输出格式:

顺序输出课程1到K的选课学生名单。格式为:对每一门课,首先在一行中输出课程编号和选课学生总数(之间用空格分隔),之后在第二行按字典序输出学生名单,每个学生名字占一行。

输入样例:

10 5ZOE1 2 4 5ANN0 3 5 2 1BOB5 5 3 4 2 1 5JOE4 1 2JAY9 4 1 2 5 4FRA8 3 4 2 5DON2 2 4 5AMY7 1 5KAT3 3 5 4 2LOR6 4 2 4 1 5

输出样例:

1 4ANN0BOB5JAY9LOR62 7ANN0BOB5FRA8JAY9JOE4KAT3LOR63 1BOB54 7BOB5DON2FRA8JAY9KAT3LOR6ZOE15 9AMY7ANN0BOB5DON2FRA8JAY9KAT3LOR6ZOE1

这样暴力排序国好像有点没水平。。。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;struct node {char name[5];};bool cmp(node a, node b) {return strcmp(a.name, b.name) < 0;}node course[2510][40010];int num[2510];int main() {int n, k;scanf("%d %d", &n, &k);int i, j;for(i = 0; i < n; i++) {char name[5];scanf("%s", name);int c;scanf("%d", &c);int id;for(j = 0; j < c; j++) {scanf("%d", &id);strcpy(course[id][num[id]++].name, name);}}for(i = 1; i <= k; i++) {sort(course[i], course[i] + num[i], cmp);}for(i = 1; i <= k; i++) {printf("%d %d\n", i, num[i]);for(j = 0; j < num[i]; j++) {printf("%s\n", course[i][j].name);}}return 0;}


0 0
原创粉丝点击