-
题目大意:给出课程的学生列表,输出学生的选课列表(1047是给出学生的选课列表,输出课程的学生列表)。
-
思路:用unordered_map存学生的课程列表(操作unordered_map比操作map更快),然后再输出被查询学生的课表,记得要先排序再输出。
(1047用了一个二维数组存课程的选课列表)
-
知识点:
- unordered_map
- string
- vector
- sort
-
代码:
#include <iostream> #include <string> #include <unordered_map> #include <vector> #include <algorithm> using namespace std; int main(){ int n, k, id, c, i, j; scanf("%d %d", &n, &k); unordered_map<string, vector<int>> students; string name; for(i = 0; i < k; i++){ scanf("%d %d", &id, &c); for(j = 0; j < c; j++){ cin >> name; // string类型只能有cin输入!!!! students[name].push_back(id); } } for(i = 0; i < n; i++){ cin >> name; sort(students[name].begin(), students[name].end()); printf("%s %d", name.c_str(), students[name].size()); for(auto it = students[name].begin(); it != students[name].end(); it++) printf(" %d", *it); printf("\n"); } return 0; }
-
总结:
-
题目1039和1047一样,要查询什么,就存储什么信息。比如1039要查询学生的选课信息,就用unordered_map存学生的课程列表,1047要查询课程的学生列表,就用二维数组存课程的选课列表;
-
细节:
string
类型的变量只能用cin
输入,不能用scanf()
输入!!! 输出可用
cout
和printf()
,但是要先把变量.c_str()
:printf("%s", str.c_str())
。
-
08-27
182
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交