• PAT 1039 Course List for Student


    1039 Course List for Student

    Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤40,000), the number of students who look for their course lists, and K (≤2,500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni​ (≤200) are given in a line. Then in the next line, Ni​ student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.

    Output Specification:

    For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student's name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.

    Sample Input:

    1. 11 5
    2. 4 7
    3. BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
    4. 1 4
    5. ANN0 BOB5 JAY9 LOR6
    6. 2 7
    7. ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
    8. 3 1
    9. BOB5
    10. 5 9
    11. AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
    12. ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9

    Sample Output:

    1. ZOE1 2 4 5
    2. ANN0 3 1 2 5
    3. BOB5 5 1 2 3 4 5
    4. JOE4 1 2
    5. JAY9 4 1 2 4 5
    6. FRA8 3 2 4 5
    7. DON2 2 4 5
    8. AMY7 1 5
    9. KAT3 3 2 4 5
    10. LOR6 4 1 2 4 5
    11. NON9 0

    总结:这道题很早就想出来了,但是一直没有写对,不是格式问题就是粗心,没有输入,还是得多练,多写题目,多积累经验

    思路:用map> 这样的数据结构来存储每个人的课表,查询者的课表总数为.size(),在将对应查询者vector中的数据排序,最后打印出来就是一个查询者需要的结果了

    代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. int main(){
    7. mapint>> m;
    8. int n,k;
    9. cin >> n >> k;
    10. int t,w;
    11. for(int i=0;i
    12. scanf("%d %d",&t,&w);
    13. string name;
    14. for(int j=0;j
    15. cin >> name;
    16. m[name].push_back(t);
    17. }
    18. }
    19. string name;
    20. for(int i=0;i
    21. cin >> name;
    22. sort(m[name].begin(),m[name].end());
    23. cout << name << ' ' << m[name].size();
    24. for(int i=0;isize();i++)
    25. printf(" %d",m[name][i]);
    26. puts("");
    27. }
    28. return 0;
    29. }

    看看大佬的代码,会有收获的!

    思路:将对应的ID转化为数字,使用vector>这样的数据结果来存储,将字母看成是一个是26进制的数,将它转化为十进制数,表示一个人的ID,查询、打印结果的过程都是差不多的了

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int getid(char *name) {
    6. int id = 0;
    7. for(int i = 0; i < 3; i++)
    8. id = 26 * id + (name[i] - 'A');
    9. id = id * 10 + (name[3] - '0');
    10. return id;
    11. }
    12. const int maxn = 26 * 26 * 26 * 10 + 10;
    13. vector<int> v[maxn];
    14. int main() {
    15. int n, k, no, num, id = 0;
    16. char name[5];
    17. scanf("%d %d", &n, &k);
    18. for(int i = 0; i < k; i++) {
    19. scanf("%d %d", &no, &num);
    20. for(int j = 0; j < num; j++) {
    21. scanf("%s", name);
    22. id = getid(name);
    23. v[id].push_back(no);
    24. }
    25. }
    26. for(int i = 0; i < n; i++) {
    27. scanf("%s", name);
    28. id = getid(name);
    29. sort(v[id].begin(), v[id].end());
    30. printf("%s %lu", name, v[id].size());
    31. for(int j = 0; j < v[id].size(); j++)
    32. printf(" %d", v[id][j]);
    33. printf("\n");
    34. }
    35. return 0;
    36. }

    好好学习,天天向上!

    我要考研

    2022.11.5

    1. /*
    2. 思路:思路比较简单,使用map> 这个数据结构存储每个学生选择的课的编号,
    3. 模拟过程就可以得出结果了
    4. */
    5. #include
    6. #include
    7. #include
    8. #include
    9. using namespace std;
    10. mapint>> m;
    11. int main(){
    12. int n,k;
    13. scanf("%d%d",&n,&k);
    14. int t,q;
    15. for(int i=0;i
    16. scanf("%d%d",&t,&q);
    17. string name;
    18. for(int j=0;j
    19. cin >> name;
    20. m[name].push_back(t);
    21. }
    22. }
    23. string name;
    24. for(int i=0;i
    25. cin >> name;
    26. cout << name;
    27. printf(" %d",m[name].size());
    28. sort(m[name].begin(),m[name].end());
    29. for(int j=0;jsize();j++) printf(" %d",m[name][j]);
    30. puts("");
    31. }
    32. return 0;
    33. }

  • 相关阅读:
    【零基础学Python】Day6 Python基本数据类型之Tuple
    Go | 函数(包)的使用
    Redis--分布式缓存--三种集群的搭建之二 哨兵模式及原理(三)
    layout viewport visual viewport ideal viewport
    OJ第三篇
    带你一起来理解布隆过滤器,带图分析~
    Visual Studio 2022 启用CodeLens - 程序抬头显示(查看字段、方法、类的引用)
    拓端tecdat|r语言中对LASSO回归,Ridge岭回归和Elastic Net模型实现
    数字集成电路设计(四、Verilog HDL数字逻辑设计方法)(二)
    Vue 数据绑定 和 数据渲染
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/126958588