[Java | 백준 11650번] 좌표 정렬하기
· 테크· Java
Java
백준 11650번 www.acmicpc.net/problem/11650
풀이
2차 배열로 입력 받아서 sort 정렬한다. 배열의 정렬시 2차 배열이기 때문에 자바에서 기본적으로 제공하는 interface 를 이용해서 comparator 객체를 생성한다. 인터페이스의 추상메소드를 override 하여 오름차순 정렬한다.
comparator 인터페이스는 comparable 인터페이스와 달리 자바에서 기존 배열 메소드를 다르게 사용하고 싶을 때 사용되는 인터페이스이다. 주로 내림차순이 sort 메소드의 기본이라면 반대로 오름차순 정렬시에 사용된다.
이 때, 두 수 중 첫번째 수로 정렬하고 그 수가 같다면 두 번째 수를 비교해서 오름차순 정렬한다.
|
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
|
package April7;
import java.util.*;
public class array_11650 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cnt = sc.nextInt();
int[][] point = new int[cnt][2];
for (int i=0; i<cnt; i++) {
point[i][0]=sc.nextInt();
point[i][1]=sc.nextInt();
}
Arrays.sort(point, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0]==o2[0])
return o1[1]-o2[1];
else
return o1[0]-o2[0];
}
});
for (int i=0; i<cnt; i++) {
System.out.println(point[i][0]+" "+point[i][1]);
}
sc.close();
}
}
|
cs |
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?
1–5 / 319