Jun's Development Journey
[BOJ] 11650 좌표 정렬하기 본문
문제
11650번: 좌표 정렬하기
첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.
www.acmicpc.net
풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Location implements Comparable<Location>{
int x, y;
public Location(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Location loc) {
// TODO Auto-generated method stub
if(this.x< loc.x)
return -1;
else if(loc.x == this.x) {
if(this.y < loc.y)
return -1;
else if(this.y == loc.y)
return 0;
else
return 1;
}
else
return 1;
}
}
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
Location[] locs = new Location[N];
//입력
for(int i=0;i<N;i++) {
StringTokenizer st = new StringTokenizer(br.readLine()," ");
locs[i] = new Location(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
}
Arrays.sort(locs);
//출력
for(int i=0;i<N;i++)
sb.append(locs[i].x+" "+locs[i].y+"\n");
System.out.print(sb);
}
}
'BOJ > Sorting' 카테고리의 다른 글
[BOJ] 10814번 나이순 정렬 (0) | 2021.02.16 |
---|