티스토리 뷰

11651. 좌표 정렬하기2

www.acmicpc.net/problem/11651

 

11651번: 좌표 정렬하기 2

첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.

www.acmicpc.net

import java.io.*;
import java.util.*;
// 210328

public class Main_BJ_11651_좌표정렬하기2 {
	public static void main(String[] args) throws Exception {
		//System.setIn(new FileInputStream("res/input_BJ_11651_좌표정렬하기2.txt"));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = null;
		
		int N = stoi(br.readLine());
		int[][] arr =new int[N][2];
		
		int x, y;
		for(int i=0; i<N; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			x = stoi(st.nextToken());
			y = stoi(st.nextToken());
			arr[i][0] = x;
			arr[i][1] = y;
		}
	
		// y 증가, x 증가순 정려
		Arrays.sort(arr, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				if(o1[1] == o2[1]) {
					return Integer.compare(o1[0], o2[0]);
				}
				return Integer.compare(o1[1], o2[1]);
			}
		});

		// 출력
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<N; i++) {
			sb.append(arr[i][0] + " " + arr[i][1] + "\n");
		}
		System.out.println(sb.toString());
		
		br.close();
	}
	
	static int stoi(String str) {
		return Integer.parseInt(str);
	}
}

'코딩테스트 > 백준' 카테고리의 다른 글

[BJ] 2108. 통계학  (0) 2021.03.29
[BJ] 1181. 단어 정렬  (0) 2021.03.28
[BJ] 11650. 좌표 정렬하기  (0) 2021.03.28
[BJ] 14502. 연구소  (0) 2021.03.26
[BJ] 1600. 말이 되고픈 원숭이  (0) 2021.03.24
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/09   »
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
글 보관함