-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path11651.java
More file actions
56 lines (46 loc) · 1.21 KB
/
Copy path11651.java
File metadata and controls
56 lines (46 loc) · 1.21 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright@2023 Jihoon Lucas Kim <jihoon.lucas.kim@gmail.com>
// 좌표 정렬하기 2
// https://www.acmicpc.net/problem/11651
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
//x, y 좌표계 정보를 담는 class
class Point implements Comparable<Point>{
public Point(int x, int y) {
this.x = x;
this.y = y;
}
int x;
int y;
@Override
public int compareTo(Point o) {
// y 오름차순으로 정렬
if (this.y > o.y) {
return 1;
} else if (this.y == o.y) { // y값이 같으면
// x 오름차순으로 정렬
if (this.x > o.x) {
return 1;
}
}
return -1;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<Point> pointArr = new ArrayList<>();
// 1. x와 y를 받아 point class 생성 후 ArrayList에 순차적으로 입력
for (int i = 0; i < N; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
pointArr.add(new Point(x, y));
}
// 2. point Class에 정의된 compareTo 함수에 맞게 정렬
Collections.sort(pointArr);
for (int i = 0; i < N; i++) {
System.out.println(pointArr.get(i).x + " " + pointArr.get(i).y);
}
}
}