티스토리 뷰

import java.io.*;
import java.util.*;

class Data {
String name;
int korean;
int english;
int math;
public Data(String name, int korean, int english, int math) {
this.name = name;
this.korean = korean;
this.english = english;
this.math = math;
}
}

class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(in.readLine());
ArrayList<Data> arrayList = new ArrayList<>();
for(int i=0; i<count; ++i) {
String[] input = in.readLine().split(" ");
arrayList.add(new Data(input[0], Integer.parseInt(input[1]), Integer.parseInt(input[2]), Integer.parseInt(input[3])));
}
Collections.sort(arrayList, new Comparator<Data>() {
@Override
public int compare(Data first, Data second) {
int result = 0;
if(first.korean < second.korean) {
result = 1;
} else {
result = -1;
}
if(first.korean == second.korean) {
if(first.english > second.english) {
result = 1;
} else {
result = -1;
}
}
if(first.korean == second.korean && first.english == second.english) {
if(first.math < second.math) {
result = 1;
} else {
result = -1;
}
}
if(first.korean == second.korean && first.english == second.english && first.math == second.math) {
if(first.name.compareTo(second.name) > 0) {
result = 1;
} else {
result = -1;
}
}
return result;
}
});
for(int i=0; i<count; ++i) {
System.out.println(arrayList.get(i).name);
}
}
}


난잡하다.

String을 compareTo로 서로 비교했을 때

양수가 나오면 사전순, 음수가 나오면 사전 역순이라는 말인 것 같다.



Swift로 위의 코드와 완전 똑같이 작성하기도 해보고 Bool을 반환하는 식으로 좀 더 짧게 작성하기도 해봤는데 둘 다 4%에서 틀렸다고 나온다...



'Algorithm > Baekjoon Online Judge' 카테고리의 다른 글

11652번 '카드'  (0) 2018.08.07
10989번 '수 정렬하기 3'  (0) 2018.08.05
10814번 '나이순 정렬'  (0) 2018.08.05
11651번 '좌표 정렬하기 2'  (0) 2018.08.05
11650번 '좌표 정렬하기'  (0) 2018.08.05
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함