程式設計團體賽的規則為:每個參賽隊由若幹隊員組成;所有隊員獨立比賽;參賽隊的成績為所有隊員的成績和;成績最高的隊獲勝。
現給定所有隊員的比賽成績,請你編寫程式找出冠軍隊。
輸入格式:
輸入第一行給出一個正整數 N(≤104 ),即所有參賽隊員總數。随後 N 行,每行給出一位隊員的成績,格式為:
隊伍編号-隊員編号 成績
,其中
隊伍編号
為 1 到 1000 的正整數,
隊員編号
為 1 到 10 的正整數,
成績
為 0 到 100 的整數。
輸出格式:
在一行中輸出冠軍隊的編号和總成績,其間以一個空格分隔。注意:題目保證冠軍隊是唯一的。
輸入樣例:
6
3-10 99
11-5 87
102-1 0
102-3 100
11-9 89
3-2 61
輸出樣例:
思路
這題也很簡單哈,直接忽略隊員編号,把隊伍編号作為下标,然後這個隊伍的總分作為其值進行累加即可~
最後在數組中尋找最大的成績就好啦,然後依次輸出它的下标和值就行了(我用的是map,用int數組應該是一個效果)~
代碼
#include<cstdio>
#include<cctype>
#include<stdlib.h>
#include<algorithm>
#include<map>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
map<int, int> m;
int main()
{
int N;
cin>>N;
for(int i=0;i<N;i++){
string tmp;
int tmpscore;
cin>>tmp>>tmpscore;
m[stoi(tmp.substr(0, tmp.find("-")))] += tmpscore;
}
int maxNum = 0, maxScore = 0;
for(map<int, int>::iterator it=m.begin();it!=m.end();it++){
if(it->second>maxScore){
maxNum = it->first;
maxScore = it->second;
}
}
cout<<maxNum<<" "<<maxScore;
return 0;
}