1032 挖掘机技术哪家强 (20分)
为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。
输入格式:
输入在第 1 行给出不超过 105 的正整数 N,即参赛人数。随后 N 行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从 1 开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。
输出格式:
在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。
输入样例:
6
3 65
2 80
1 100
2 70
3 40
3 0
输出样例:
2 150
第一版(17分,最后一个测试点运行超时):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int totalNum = Integer.parseInt(s);
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < totalNum; i++) {
String s1 = br.readLine();
String[] s2 = s1.split(" ");
if (map.get(Integer.parseInt(s2[0])) == null) {
map.put(Integer.parseInt(s2[0]), Integer.parseInt(s2[1]));
} else {
map.put(Integer.parseInt(s2[0]), map.get(Integer.parseInt(s2[0])) + Integer.parseInt(s2[1]));
}
}
ArrayList<HashMap.Entry<Integer, Integer>> hashMaps = new ArrayList<>(map.entrySet());
Collections.sort(hashMaps, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
System.out.println(hashMaps.get(0).getKey() + " " + hashMaps.get(0).getValue());
}
}
他人版本:
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[]args)throws IOException{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer st=new StreamTokenizer(bf);
int maxId=0;
st.nextToken();
int n=(int)st.nval;
int []d=new int[n+1];
for(int i=0;i<n;++i){
st.nextToken();
int id=(int)st.nval;
st.nextToken();
d[id]+=(int)st.nval;
if(d[id]>d[maxId]){
maxId=id;
}
}
System.out.printf("%d %d",maxId,d[maxId]);
}
}