문제 : 좌표 정렬하기
시간 제한 : 1 sec
메모리 제한 : 256 MB
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
|
import java.util.*;
class Point implements Comparator<Point>{
int xpos;
int ypos;
Point(){
}
Point(int x, int y){
this.xpos = x;
this.ypos = y;
}
public int compare(Point o1, Point o2) {
// TODO Auto-generated method stub
if(o1.xpos==o2.xpos) {
return o1.ypos-o2.ypos;
}else
return o1.xpos-o2.xpos;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Point> p = new ArrayList<>();
for (int i = 0; i < n; i++) {
p.add(new Point(sc.nextInt(),sc.nextInt()));
}
Collections.sort(p, new Point());
for (int i = 0; i < n; i++) {
System.out.println(p.get(i).xpos+" "+p.get(i).ypos);
}
}
}
|
cs |
배열로 주구장창 돌렸는데 시간초과로 화나서 미뤄뒀던 문제,,,
오늘 갑자기 풀어볼까 하고 아예 새로 다시 풀었더니 풀렸다
Comparator 사용하고 새로운 Class도 만들어서 푼 문제는 백준에서 처음인 듯!
시간 초과로 안 풀렸던 문제 풀리면 화나는데 기분 좋아 :( 이게 뭔 기분일까
'algorithm > 문제 풀이' 카테고리의 다른 글
백준 17219번 (비밀번호 찾기) 자바 Java11 (0) | 2021.01.11 |
---|---|
열혈 C++ 프로그래밍 클래스 정의 문제 (0) | 2021.01.10 |
백준 2869(달팽이는 올라가고 싶다) C++ (0) | 2021.01.07 |
백준 2164번 (카드2) 자바 Java11 (0) | 2021.01.04 |
백준 1436번 (영화감독 숌) 자바 Java11 (0) | 2021.01.02 |