배운점
Comparator를 사용하여 정렬하면 쉽다. 자주 활용해야겠다. ArrayList를 이용하는 것에 익숙해지자
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 | package 정렬; import java.io.*; import java.util.*; class Point{ int x,y; public Point(int x, int y) { this.x = x; this.y = y; } } public class 좌표정렬하기 { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); Comparator<Point> com = null; ArrayList<Point> list = new ArrayList<Point>(); com = new Comparator<Point>() { @Override public int compare(Point o1, Point o2) { if (o1.x > o2.x) return 1; else if (o1.x == o2.x) { if (o1.y > o2.y) return 1; else return -1; } else return -1; } }; for (int i = 0; i < n; i++) { String[] in = br.readLine().split(" "); list.add(new Point(Integer.parseInt(in[0]), Integer.parseInt(in[1]))); } list.sort(com); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).x+" "+list.get(i).y); } } } | cs |
'IT > 알고리즘' 카테고리의 다른 글
백준 15649 N 과M(1) (0) | 2020.02.20 |
---|---|
백준10989 수 정렬하기3 (0) | 2020.02.18 |
백준 1427 소트인사이드 (1) | 2020.02.16 |
백준 11729 하노이탑 이동순서 문제 (0) | 2020.02.15 |
백준 통계학문제 2108 자바 (0) | 2020.02.15 |