PTA B1047 编程团体赛 C++/Python3
# 默认分类
题目描述
编程团体赛的规则为:每个参赛队由若干队员组成;所有队员独立比赛;参赛队的成绩为所有队员的成绩和;成绩最高的队获胜。
现给定所有队员的比赛成绩,请你编写程序找出冠军队。
输入格式
输入第一行给出一个正整数 N(≤10 4),即所有参赛队员总数。随后 N 行,每行给出一位队员的成绩,格式为:队伍编号-队员编号 成绩,其中队伍编号为 1 到 1000 的正整数,队员编号为 1 到 10 的正整数,成绩为 0 到 100 的整数。
输出格式
在一行中输出冠军队的编号和总成绩,其间以一个空格分隔。注意:题目保证冠军队是唯一的。
样例 #1
样例输入 #1
6
3-10 99
11-5 87
102-1 0
102-3 100
11-9 89
3-2 61
样例输出 #1
11 176
C++
#include <bits/stdc++.h>
using namespace std;
int n;
signed main() {
//定义一个map,存队伍编号和总成绩
map<int,int> m;
cin>>n;
while(n--) {
string temp;
int f;
cin>>temp>>f;
//以'-'分割,只需要前面的队伍号,增加当前队伍分数
m[stoi(temp.substr(0,temp.find('-')))]+=f;
}
//ans1是队伍编号,ans2是总分数
int ans1,ans2=0;
//用iterator迭代读每一个队伍分数
for(auto it=m.iterator(); it!=m.end(); it++) {
if(it->second >ans2) {
ans2=it->second;
ans1=it->first;
}
}
cout<<ans1<<" "<<ans2;
return 0;
}
Python3/Pypy3
n = int(input())
dic = {}
for i in range(n):
temp = input().split()
team = temp[0].split('-')
score = int(temp[1])
if team[0] not in dic:
dic[team[0]] = score
else:
dic[team[0]] += score
print(max(dic, key=dic.get), dic[max(dic, key=dic.get)])