티스토리 뷰
import java.io.*;
import java.util.*;
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
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<Point> arrayList = new ArrayList<>();
for(int i=0; i<count; ++i) {
String[] input = in.readLine().split(" ");
int x = Integer.parseInt(input[0]);
int y = Integer.parseInt(input[1]);
arrayList.add(new Point(x, y));
}
Collections.sort(arrayList, new Comparator<Point>() {
@Override
public int compare(Point o1, Point o2) {
if(o1.y == o2.y) {
return -1;
}
return -1;
}
});
for(int i=0; i<count; ++i) {
System.out.print(arrayList.get(i).x + " ");
System.out.print(arrayList.get(i).y);
System.out.println();
}
}
}
좌표 정렬하기 문제와 비슷하게 정렬 조건만 바꾸어 풀면 된다.
compare 재정의 구현에 대하여.
양수를 반환하면 파라미터 중 앞에 온 것이 큰 것
0을 반환하면 같은 것
음수를 반환하면 파라미터 중 뒤에 온 것이 큰 것
그러므로 Swift 코드랑 똑같이 구현해도 문제 없다.
> : 1
== : 0
< : -1
let count = Int(readLine()!)!
var array = [(Int, Int)]()
for _ in 0..<count {
let input = readLine()!.split(separator: " ").map { Int($0)! }
array.append((input.first!, input.last!))
}
array.sort { (first, second) -> Bool in
if first.1 == second.1 {
return first.0 < second.0
}
return first.1 < second.1
}
for i in array {
print(i.0, i.1)
}
'Algorithm > Baekjoon Online Judge' 카테고리의 다른 글
10825번 '국영수' (0) | 2018.08.05 |
---|---|
10814번 '나이순 정렬' (0) | 2018.08.05 |
11650번 '좌표 정렬하기' (0) | 2018.08.05 |
2751번 '수 정렬하기 2' (0) | 2018.08.04 |
2004번 '조합 0의 개수' (0) | 2018.08.01 |
댓글