티스토리 뷰
문제
코드
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); ArrayPoint[] ap = new ArrayPoint[n]; for (int i = 0; i < n; i++) { String[] arr = br.readLine().split(" "); int x = Integer.parseInt(arr[0]); int y = Integer.parseInt(arr[1]); ap[i] = new ArrayPoint(x, y); } Arrays.sort(ap); // 오름차순 StringBuilder sb = new StringBuilder(); for (ArrayPoint p : ap) { sb.append(p.x + " " + p.y + "\n"); } System.out.println(sb); } } class ArrayPoint implements Comparable{ int x, y; public ArrayPoint(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(ArrayPoint o) { // this.x < o.x == -1 : 음수 // this.x = o.x == 0 : 0 // this.x > o.x == 1 : 양수 // 좌표를 x좌표가 증가하는 순으로, x좌표가 같으면 y좌표가 증가하는 순서로 정렬 if (this.x < o.x) { return -1; } else if (this.x == o.x) { if (this.y == o.y) { return 0; } else if (this.y < o.y) { return -1; } else { return 1; } } else { return 1; } } }
출처
'알고리즘 > 백준알고리즘(JAVA)' 카테고리의 다른 글
#10826번 : 피보나치 수 4 (0) | 2018.09.16 |
---|---|
#11286번 : 절대값 힙 (0) | 2018.09.15 |
#11279번 : 최대 힙 (0) | 2018.09.15 |
#1927번 : 최소 힙 (0) | 2018.09.14 |
#1764번 : 듣보잡 (0) | 2018.09.12 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 1260
- #kotlin
- mssql
- a^b
- GCD 합
- 조세퍼스 문제
- OpenCV
- 2743
- 최대공약수와 최소공배수
- 1158
- Eclipse
- kotlin
- 함수형사고 Kotlin Java
- #android #motionlayout
- 10757
- 10826
- 알고리즘
- javacv
- 큰 수 A+B
- 1237
- 문자열
- 영상처리
- 피보나치 수 4
- constraintlayout
- algorithm
- 10827
- 10828
- 자동타입
- 단어 길이 재기
- algorihtm
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함