PAT甲级 1153 Decode Registration Card of PAT (25分) map/C++

1153 Decode Registration Card of PAT (25分)

题目大意:给定准考证号和成绩,按照下列查询方式查询:

  1. 给定level,找到该等级考试的所有学生,按照分数降序排序,相等则按准考证号升序排序;
  2. 给定site,找到site的所有成绩之和,并输出人数;
  3. 给定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";
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值