PAT甲级1153

本文针对PAT甲级1153题提供详细解答,采用C++实现,利用unordered_map进行高效数据处理。文章讲解了如何根据不同查询代码输出特定结果,包括按分数排序的考生信息、特定地点考生总分及指定日期各地区考生数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PAT甲级1153

题目大意:给出每个考生的卡号以及成绩,按照一定规则查询。规则如下:如果输入的查询代码是1则输出之后要查询的等级中的考生且按照分数从大到小排列,如果相同则按照卡号字母序从小到大排列;如果输入的查询代码是2则输出这个地点所有考生的总和(不论什么等级);如果输入的查询代码是3则输出该日期内每个地区的考生人数。
注意要用unordered_map还有cout全部用printf代替否则后面两个测试点会超时。

#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;

struct node{
	string key;
	int value;
};
bool cmp1(node &a,node &b){
	if(a.value!=b.value) return a.value>b.value;
	else return a.key<b.key;
}
bool cmp2(node &a,node &b){
	if(a.value!=b.value) return a.value>b.value;
	else return a.key<b.key;
}
int main(){
	int n,m,score,queryid,totalnum=0,totalscore=0;
	string card,querycontent;
	scanf("%d%d",&n,&m);
	vector<node> t,v;
	unordered_map<string,int> site;
	for(int i=0;i<n;i++){
		cin>>card>>score;
		node tmp;
		tmp.key=card;
		tmp.value=score;
		t.push_back(tmp);
	}
	sort(t.begin(),t.end(),cmp1);
	for(int i=0;i<m;i++){
		cin>>queryid>>querycontent;
		cout<<"Case "<<i+1<<": "<<queryid<<" "<<querycontent<<endl;
		if(queryid==1){
			totalnum=0;
			for(auto it:t)
			if(it.key[0]==querycontent[0]){
					printf("%s %d\n",it.key.c_str(),it.value);
					totalnum++;
				}
			if(totalnum==0) printf("NA\n");
		}else if(queryid==2){
			totalnum=0,totalscore=0;
			for(auto it:t)
			if(it.key.substr(1,3)==querycontent){
					totalnum++;
					totalscore+=it.value;
				}
			if(totalnum==0) printf("NA\n");
			else printf("%d %d\n",totalnum,totalscore);
		}else{
			v.clear();
			site.clear();
			for(auto it:t)
				if(it.key.substr(4,6)==querycontent)
					site[it.key.substr(1,3)]++;
			for(auto it:site){
				node tmp;
				tmp.key=it.first;
				tmp.value=it.second;
				v.push_back(tmp);
			}
			if(v.size()!=0){
				sort(v.begin(),v.end(),cmp2);
				for(auto it:v)
					printf("%s %d\n",it.key.c_str(),it.value);
			}else printf("NA\n");
		}
	}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值