PAT 甲级 A1039 (2019/02/22)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = 40010;
const int MAXM = 26*26*26*10 + 1;
vector<int> selectCourses[MAXM];
//vector数组,selectCourses[0] ~ selectCourses[MAXM - 1]中的每一个都是一个容器 
int getID(char name[]){
    int ID = 0;
    for(int i = 0; i < 3; i++){
        ID = ID * 26 + (name[i] - 'A');
    }
    return ID * 10 + (name[3] - '0');
}
int main(){
    int N, K;
    scanf("%d %d", &N, &K);     //人数及选课数 
    for(int i = 0; i < K; i++){
        int courseIndex, stuNumber;
        scanf("%d %d", &courseIndex, &stuNumber);   //输入课程编号及选课人数 
        for(int j = 0; j < stuNumber; j++){
            char StuName[5];
            scanf("%s", StuName);   //输入选课学生姓名 
            int StuID = getID(StuName);     //将姓名散列为一个整数作为编号 
            selectCourses[StuID].push_back(courseIndex);    //将课程编号加入学生选课数组 
        }
    }
    for(int k = 0; k < N; k++){ //N个查询 
        char StuName[5];
        scanf("%s", StuName);   //学生姓名 
        int getStuID = getID(StuName);  //获得学生编号 
        sort(selectCourses[getStuID].begin(), selectCourses[getStuID].end());   //从小到大排序 
        printf("%s %d", StuName, selectCourses[getStuID].size());   //姓名,选课数 
        for(int l = 0; l < selectCourses[getStuID].size(); l++){
            printf(" %d",selectCourses[getStuID][l]);   //选课编号 
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zjsaipplp/p/10425228.html