[백준 11651번 : 좌표 정렬하기2] java 풀이

Elmo·2022년 7월 27일
0

[백준] 알고리즘

목록 보기
13/39

이전 문제인 <11650번 : 좌표 정렬하기> 와 거의 비슷한 문제이다.
바뀐 건 y좌표를 우선으로 정렬하고, y좌표가 같으면 x좌표를 정렬하는 것이다.

따라서 특별한 설명은 필요없을듯..

🔔 java 풀이

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args){
	
		Scanner sc = new Scanner(System.in);
		int N= sc.nextInt();
		int point[][]=new int[N][2];
		for(int i=0; i<N; i++) {
			point[i][0]=sc.nextInt();
			point[i][1]=sc.nextInt();
		}
		
		Arrays.sort(point,(e1,e2)->{
			if(e1[1]==e2[1])
				return e1[0]-e2[0];
			else
				return e1[1]-e2[1];
		});
		
		for(int i=0; i<N; i++) 
			System.out.println(point[i][0]+" "+point[i][1]);
	}
}

profile
엘모는 즐거워

0개의 댓글