【算法笔记】A1047 Student List for Course

https://pintia.cn/problem-sets/994805342720868352/problems/994805433955368960

题意

  给出每个学生的选课情况,输出每节课选课的学生。

思路

  参考了上机指南的代码,用char[n][5]存放学生姓名,vector<int> stulList[] 存放选每门课的学生编号,通过编号对应的姓名在vector数组里排序,妙啊

 code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char name[40010][5];
 4 vector<int> stuList[2510];
 5 bool cmp(int a, int b){
 6     return strcmp(name[a], name[b]) < 0;
 7 }
 8 int main(){
 9     int n, k, c, courseId;
10     scanf("%d%d", &n, &k);
11     for(int i = 0; i < n; i++){
12         scanf("%s %d", name[i], &c);
13         for(int j = 0; j < c; j++){
14             scanf("%d", &courseId);
15             stuList[courseId].push_back(i);
16         }
17     }
18     for(int i = 1; i <= k; i++){
19         int size = stuList[i].size();
20         printf("%d %d\n", i, size);
21         sort(stuList[i].begin(), stuList[i].end(), cmp);
22         for(int j = 0; j < size; j++){
23             printf("%s\n", name[stuList[i][j]]);
24         }
25     }
26     return 0;
27 }

猜你喜欢

转载自www.cnblogs.com/chunlinn/p/10694638.html