傳送門
https://pintia.cn/problem-sets/994805260223102976/problems/994805277163896832
題目
編程團(tuán)體賽的規(guī)則為:每個(gè)參賽隊(duì)由若干隊(duì)員組成六敬;所有隊(duì)員獨(dú)立比賽渐北;參賽隊(duì)的成績?yōu)樗嘘?duì)員的成績和;成績最高的隊(duì)獲勝浴韭。
現(xiàn)給定所有隊(duì)員的比賽成績弦疮,請你編寫程序找出冠軍隊(duì)诱建。
輸入格式:
輸入第一行給出一個(gè)正整數(shù)N(<=10000)钮蛛,即所有參賽隊(duì)員總數(shù)阅签。隨后N行掐暮,每行給出一位隊(duì)員的成績,格式為:“隊(duì)伍編號(hào)-隊(duì)員編號(hào) 成績”政钟,其中“隊(duì)伍編號(hào)”為1到1000的正整數(shù),“隊(duì)員編號(hào)”為1到10的正整數(shù)樟结,“成績”為0到100的整數(shù)养交。
輸出格式:
在一行中輸出冠軍隊(duì)的編號(hào)和總成績,其間以一個(gè)空格分隔瓢宦。注意:題目保證冠軍隊(duì)是唯一的碎连。
輸入樣例:
6
3-10 99
11-5 87
102-1 0
102-3 100
11-9 89
3-2 61
輸出樣例:
11 176
分析
先建個(gè)表存放各隊(duì)伍的成績,下標(biāo)為隊(duì)伍編號(hào)驮履,題目輸入的隊(duì)員編號(hào)其實(shí)是沒有用的鱼辙,把每個(gè)成績都加到對應(yīng)的隊(duì)伍上,然后輸出最大值和最大值的隊(duì)伍就行玫镐。
源代碼
//C/C++實(shí)現(xiàn)
#include <iostream>
using namespace std;
int queue[1001];
int main(){
int n;
scanf("%d", &n);
int max = 0, max_index = -1;
for(int i = 0; i < n; ++i){
int queueNo, memberNo, grade;
scanf("%d-%d %d", &queueNo, &memberNo, &grade);
// memberNo沒卵用的倒戏,就來客串一下
queue[queueNo] += grade;
if(queue[queueNo] > max){
max = queue[queueNo];
max_index = queueNo;
}
}
printf("%d %d\n", max_index, max);
return 0;
}
//Java實(shí)現(xiàn)
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String s = bufferedReader.readLine();
int n = 0;
try{
n = Integer.valueOf(s);
}catch (Exception e){
System.exit(0);
}
if(n >0 && n <= 10000){
int[] array = new int[1001];
int max = 0,max_index = 0,array_max_index = 0;
int team = 0,no = 0,grade = 0;
for(int i=0;i<n;i++){
String ss = bufferedReader.readLine();
String[] arraySs = ss.split(" ");
String[] sp = arraySs[0].split("-");
try {
team = Integer.valueOf(sp[0]);
no = Integer.valueOf(sp[1]);
grade = Integer.valueOf(arraySs[1]);
}catch (Exception e){
System.exit(0);
}
if(team < 1 || team > 1000 || no < 1 || no > 10 || grade < 0 || grade > 100 ){
System.exit(0);
}
array[team] += grade;
if(team > array_max_index){
array_max_index = team;
}
}
for(int k=0;k<array_max_index+1;k++){
if(array[k]>max) {
max = array[k];
max_index = k;
}
}
System.out.println(max_index+" "+max);
}
}
}