문제에 대한 자세한 정보는 백준 | 10825번 : 국영수에서 확인할 수 있다.
import java.io.*;
import java.util.*;
class Student implements Comparable<Student> {
String name;
int kor, eng, math;
public Student(String name, int kor, int eng, int math) {
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
}
@Override
public int compareTo(Student o) {
if (this.kor == o.kor) {
if (this.eng == o.eng) {
if (this.math == o.math) {
return this.name.compareTo(o.name);
}
return o.math - this.math;
}
return this.eng - o.eng;
}
return o.kor - this.kor;
}
@Override
public String toString() {
return this.name + "\n";
}
}
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.valueOf(br.readLine());
Student[] student = new Student[N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String name = st.nextToken();
int kor = Integer.valueOf(st.nextToken());
int eng = Integer.valueOf(st.nextToken());
int math = Integer.valueOf(st.nextToken());
student[i] = new Student(name, kor, eng, math);
}
Arrays.sort(student);
for (int i = 0; i < N; i++) {
bw.write(student[i].toString());
}
br.close();
bw.close();
}
}
메모리 : 68656KB
시간 : 708ms
compareTo(Student o) 메소드만 잘 작성하면 크게 어려운 문제는 아니었다.
그런데 문제의 조건 중
모든 점수가 같으면 이름이 사전 순으로 증가하는 순서
이 조건을 이름 맨 앞 글자만 비교하면 되겠다라고 착각했다. 바보같다.
조금 더 꼼꼼히 생각하는 습관을 길러야겠다.