PAT 乙级真题 1032 挖掘机技术哪家强 (附测试点2)

为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。

输入格式:

输入在第1行给出不超过10^5的正整数N,即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。

输出格式:

在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。

输入样例

6
3 65
2 80
1 100
2 70
3 40
3 0

输出样例:2 150

个人思路1:用结构体存放学校名字和成绩,容器封装,最后排序输出第一个数据。

个人思路2:使用map,学校名为key,成绩为value,最后排序输出第一个元素(熟练使用map有助于以后的做题,知识点比较多,请查阅学习)

关于测试点2:是只有一个学校且学校成绩为 0 例如: 1  5  0    正确输出为:5  0

AC代码1:

#include<bits/stdc++.h>
#include<string>
using namespace std;
typedef struct node{
	int name;
	long score;
};
int cmp(struct node a,struct node b){
	return a.score > b.score;
}//排序
long N;

int main(void)
{
	cin>>N;
	int K = N;
	vector<node> v(100010);
	int sname,sscore;
	while(K--)
	{
		cin>>sname>>sscore;
		v[sname].name = sname;
		v[sname].score += sscore;
	}
	if(N == 1) cout<<v[sname].name<<" "<<v[sname].score; //如果只有一个学校,则直接输出即可
	else{
		sort(v.begin(),v.end(),cmp);
		cout<<v[0].name<<" "<<v[0].score;
	} 
	return 0;
}

AC代码2:

#include<bits/stdc++.h>
#include<map>
using namespace std;
long N;
map<int,int> Map;
 
int cmp(pair<int,int> a, pair<int,int> b) {
	return a.second > b.second;
}//排序

int main(void){
	cin>>N;
	int sname,sscore;
	map<int,int>::iterator it;
	while(N--){
		cin>>sname>>sscore;
		if(Map[sname] == 0) 
			Map[sname] = sscore;
			
		else if(Map[sname] != 0)
			for(it = Map.begin();it != Map.end(); ++it)
				if(it->first == sname) 
					Map[sname] = it->second + sscore;
		//学校名相同,分数相加
		
	}
	vector<pair<int,int>> vec;
	for(it = Map.begin();it != Map.end(); ++it)
		vec.push_back(pair<int,int>(it->first,it->second));
	//map不能直接用sort排序,先装进pair,在放入容器,最后排序
	sort(vec.begin(),vec.end(),cmp);
	cout<<vec[0].first<<" "<<vec[0].second;
	return 0;
}

此处附上关于map排序的blog:https://blog.csdn.net/qq_42376204/article/details/84112637?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163049524416780366575735%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163049524416780366575735&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v29_ecpm-1-84112637.pc_search_result_control_group&utm_term=C%2B%2B+%E6%8E%92%E5%BA%8Fmap%E7%9A%84value&spm=1018.2226.3001.4187

最后附上柳神的代码链接:https://blog.csdn.net/liuchuo/article/details/51994410?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163049347216780274145499%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=163049347216780274145499&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v2~rank_v29-1-51994410.pc_v2_rank_blog_default&utm_term=1032&spm=1018.2226.3001.4450

思路和代码仅供参考,请对比学习。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

求求无BUG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值