1153 Decode Registration Card of PAT (25分)
题目大意:给定准考证号和成绩,按照下列查询方式查询:
- 给定level,找到该等级考试的所有学生,按照分数降序排序,相等则按准考证号升序排序;
- 给定site,找到site的所有成绩之和,并输出人数;
- 给定date,找到每个site的人数,按人数降序输出,相等则按site升序输出。
输入输出用scanf、printf,并且使用unordered_map代替map,否则会超时
自己写完以后,再看柳神的代码,还是一如既往的强
下面是我的题解:
#include<iostream> //输入输出流头文件
#include<algorithm> //C++标准模板库的函数
#include<unordered_map> //无序的map映射容器
#include<vector> //变长数组容器
#include<string> //C++string类
using namespace std; //标准命名空间
struct tests{
string level, site,date;
int score;
};
int n,m;
bool cmp(pair<string,int> a,pair<string ,int> b){
return a.second==b.second?a.first<b.first:a.second>b.second;
}
void checkQuiry(unordered_map<string,tests> test);
int main(){ //主函数
#ifdef ONLINE_JUDGE //如果有oj系统(在线判定),则忽略文件读入,否则使用文件作为标准输入
#else
freopen("1.txt", "r", stdin); //从1.txt输入数据
#endif
cin>>n>>m;
unordered_map<string ,tests> test;
for(int i=0;i<n;i++){
string id;
int s;
id.resize(13);
scanf("%s %d",&id[0],&s);
test[id].level=id.substr(0,1);
test[id].site=id.substr(1,3);
test[id].date=id.substr(4,6);
test[id].score=s;
}
checkQuiry(test);
return 0; //返回0,如果不返回0,PAT会报错
}
void checkQuiry(unordered_map<string,tests> test){
for(int i=0;i<m;i++){
int type,count=0,sum=0;
unordered_map<string,int> type3;
string term;
cin>>type>>term;
unordered_map<string,tests>::iterator iter=test.begin();
cout<<"Case "<<i+1<<": "<<type<<' '<<term<<endl;
for(;iter!=test.end();iter++){
if(type==1&&iter->second.level==term&&++count){
type3[iter->first]=iter->second.score;
}
else if(type==2&&iter->second.site==term&&++count){
sum+=iter->second.score;
}
else if(type==3&&iter->second.date==term&&++count){
type3[iter->second.site]++;
}
}
if(count){
if(type==2) cout<<count<<' '<<sum<<endl;
else if(type==3||type==1){
vector<pair<string,int>> v(type3.begin(),type3.end());
sort(v.begin(),v.end(),cmp);
for(int j=0;j<v.size();j++)
printf("%s %d\n",&v[j].first[0],v[j].second);//cout<<v[j].first<<' '<<v[j].second<<endl;
}
}
else cout<<"NA\n";
}
}