The Kkang's man

[ 자바 /Java ] 백준 11650 : 좌표 정렬하기 본문

알고리즘/정렬

[ 자바 /Java ] 백준 11650 : 좌표 정렬하기

정낑깡 2021. 8. 1. 19:31

문제


 

 


 

풀이


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class CoordinAlign_11650 {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		int N = Integer.parseInt(br.readLine());
		int[][] arr = new int[N][2];
		
		for(int i=0; i<N; i++) {
			st = new StringTokenizer(br.readLine());
			
			int x = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());
			
			arr[i][0] = x;
			arr[i][1] = y;
		}
		
		Arrays.sort(arr, new Comparator<int[]>() {
			public int compare(int[] c1, int[] c2) {
				if(c1[0] == c2[0]) {
					return c1[1] - c2[1];
				}
				return c1[0] - c2[0];
			}
		});
		
		for(int i=0; i<N; i++) {
			System.out.println(arr[i][0] + " " + arr[i][1]);
		}
	}
}
Comments