给出一组学生的准考证号和成绩,准考证号包含了等级(乙甲顶),考场号,日期,和我的编号信息,并有三种查询方式ios
首先咱们使用cards数组将全部的信息保存下来,也无需进行预处理(作预处理可能耗时更长),而后对于type的取值进行一一处理。算法
unordered_map<string,int> mymap
存储当前term日期下的每个考场和人数的映射,而后遍历cards集合,根据当前日期的考试信息统计每个考场 的人数,而后使用type3数组保存term日期下全部的考场编号和其考试人数,这里复用了Card保存对应信息(类型都同样,排序函数同样),而后对于type3进行排序输出便可。#include<cstdio> #include<vector> #include<string> #include<iostream> #include<unordered_map> #include<algorithm> using namespace std; struct Card{ string id; int score; }; vector<Card> cards; bool cmpByScore(const Card& a,const Card& b){ return a.score!=b.score?a.score>b.score:a.id<b.id; } int main(){ int N,M; scanf("%d %d",&N,&M); Card card; for (int i = 0; i < N; ++i) { cin>>card.id>>card.score; cards.push_back(card); } int type; string term; for(int i=0;i<M;++i){ cin>>type>>term; printf("Case %d: %d %s\n",i+1,type,term.c_str()); if(type==1){ // 按照分数逆序输出term level的全部card信息 vector<Card> type1; for(auto &item:cards){ if(item.id[0]==term[0]){ type1.push_back(item); } } if(type1.empty()){ printf("NA\n"); continue; } sort(type1.begin(),type1.end(),cmpByScore); for(auto &item:type1){ printf("%s %d\n",item.id.c_str(),item.score); } } else if(type==2){ // 输出全部term考场的总人数和总成绩 int num = 0; int total = 0; for(auto &item:cards){ if(item.id.substr(1,3)==term){ ++num; total += item.score; } } if(num==0){ printf("NA\n"); continue; } printf("%d %d\n",num,total); } else if(type==3){ // 输出term日期中每个考场编号和其考试人数,根据考试人数逆序排列 unordered_map<string,int> mymap;// 存储当前term日期下的每个考场和人数的映射 for(auto &item:cards){ if(item.id.substr(4,6)==term){ ++mymap[item.id.substr(1,3)]; } } if(mymap.empty()){ printf("NA\n"); continue; } // 保存term日期下全部的考场编号和其考试人数,这里复用了Card保存对应信息(类型都同样,排序函数同样) vector<Card> type3; for(auto & it : mymap){ type3.push_back(Card{it.first,it.second}); } sort(type3.begin(),type3.end(),cmpByScore); for(auto &item:type3){ printf("%s %d\n",item.id.c_str(),item.score); } } else { printf("NA\n"); } } return 0; }