zoukankan      html  css  js  c++  java
  • PAT甲题题解-1039. Course List for Student (25)-建立映射+vector

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
    http://www.cnblogs.com/chenxiwenruo/p/6789157.html
    特别不喜欢那些随便转载别人的原创文章又不给出链接的
    所以不准偷偷复制博主的博客噢~~

    题意:给出要查询的n个学生,k个课程
    接下来对于k门课,给出其id和学生数量,以及对应的学生名字
    租后给出n个查询的学生名字,让你输出其选的课程数量和对应的课程id,id从小到大排序。

    题目简单,然而建立映射的想法不错~~推荐
    一开始发生段错误,才发现n的范围只是询问学生的范围
    实际上学生最多会有200*2500=500000个
    然而结果变成超时了,估计是map的原因
    受启发,名字实际上是ABC+数字组成,所以可以根据这个来建立与id的映射关系
    最多有26*26*26*10个学生。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <string.h>
    #include <cmath>
    #include <map>
    #include <vector>
    using namespace std;
    int n,k;
    map<string,int>name_id;
    vector<int> stu[26*26*26*10+5];
    
    int getId(char*str){
        int id=0;
        for(int i=0;i<3;i++){
            id=id*26+str[i]-'A';
        }
        id=id*10+str[3]-'0';
        return id;
    }
    int main()
    {
        string name;
        char str[5];
        int id,idx,num;
        int cnt=0;
        scanf("%d %d",&n,&k);
        for(int i=0;i<k;i++){
            scanf("%d %d",&idx,&num);
            for(int j=0;j<num;j++){
                scanf("%s",str);
                //name=str;
                //cin>>name;
                //if(name_id[name]==0){
                //   name_id[name]=++cnt;
                //}
                id=getId(str);
                //stu[name_id[name]].push_back(idx);
                stu[id].push_back(idx);
            }
        }
        for(int i=0;i<n;i++){
            scanf("%s",str);
            //name=str;
            printf("%s",str);
            int id=getId(str);
            printf(" %lu",stu[id].size());
            sort(stu[id].begin(),stu[id].end());
            for(int j=0;j<stu[id].size();j++){
                printf(" %d",stu[id][j]);
    
            }
            printf("
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    C#编码标准--编码习惯
    课程九,课堂测试
    JAVAweb 分级测试
    第八周 课堂报告
    javaweb界面
    12月9日,第一次自查报告
    课程管理系统后台JAVA代码
    《程序员修炼之道+从小工到专家》读后有感
    12月9日 自查后续
    课程管理系统JAVAweb前端代码
  • 原文地址:https://www.cnblogs.com/chenxiwenruo/p/6789157.html
Copyright © 2011-2022 走看看