为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。
输入格式:
输入在第1行给出不超过10的正整数N,即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。
输出格式:
在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。
输入样例:
6
3 65
2 80
1 100
2 70
3 40
3 0
输出样例:
2 150
package com.hone.basical; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner; /**
* 原题目:https://www.patest.cn/contests/pat-b-practise/1030
* @author Xia
* 和前面一样,通过建立一个school类,定义id score(准确的数是一个学生类)
* 先将相同的id的score进行合并,然后排列,取最大的一个对象。
* List的应用
* method 1
*/
public class basicalLevel1032BestSchool{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
List<school> slist = new ArrayList<>();
for (int i = 0; i < n; i++) {
school sl = new school();
sl.id = s.nextInt();
sl.score = s.nextInt();
slist.add(sl);
}
for (int i = 0; i < slist.size(); i++) {
for (int j = slist.size()-1; j > i; j--) {
if (slist.get(i).id == slist.get(j).id) {
slist.get(i).setScore(slist.get(i).score+slist.get(j).score);
slist.remove(j);
}
}
}
Collections.sort(slist, new compareByscore()); System.out.println(slist.get(slist.size()-1).id+" "+
slist.get(slist.size()-1).score);
} } class compareByscore implements Comparator{ @Override
public int compare(Object o1, Object o2) {
school s1 = (school) o1;
school s2 = (school) o2;
return s1.score-s2.score;
}
} class school{
int id;
int score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
} }
package com.hone.basical; import java.util.Scanner; /**
* 原题目:https://www.patest.cn/contests/pat-b-practise/1032
* @author Xia
* 方法2
* 思路:用一个school[]数组来存储每一个学校的最终得分,但是这样容易造成超时。因为数组必须建得足够的大。
*/
public class basicalLevel1032BestSchoolImprove{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt(); //学生总人数
int[] school = new int[100001];
for (int i = 0; i < n; i++) {
int id = s.nextInt();
int score = s.nextInt();
school[id]+=score;
}
int max = -1;
int index = 0;
for (int i = 0; i < school.length; i++) {
if (school[i] > max) {
max = school[i];
index = i;
}
}
System.out.println(index+" "+school[index]);
}
}