为了用事实说明挖掘机技术到底哪家强,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;
}
思路和代码仅供参考,请对比学习。