본문 바로가기
Algorithm/BaekJoon[Java]

백준 11651

by 씨니 2022. 3. 22.
728x90

▶11651문제 - 좌표 정렬하기2

 

▶풀이방법

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int count = Integer.parseInt(br.readLine()); //입력받을 개수 입력
		int[][] arr = new int[count][2];
		
		for(int i = 0; i < count; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine(), " ");
			arr[i][0] = Integer.parseInt(st.nextToken());
			arr[i][1] = Integer.parseInt(st.nextToken());
		}
		
		Arrays.sort(arr, new Comparator<int[]>() {
			public int compare(int[] o1, int[] o2) {
				if(o1[1] == o2[1]) {
					return o1[0] - o2[0];
				}else {
					return o1[1] - o2[1];
				}
			}
		});
		
		for(int i = 0; i < count; i++) {
			bw.write(String.valueOf(arr[i][0] + " " + arr[i][1]) + "\n");
		}
		
		bw.flush();
		bw.close();
		br.close();
	}
}

 

++ Arrays.sort 람다식이 Arrays.sort Comparator방식보다 빠름.

++배열 주의할것!!

728x90

'Algorithm > BaekJoon[Java]' 카테고리의 다른 글

백준 10989  (0) 2022.03.23
백준 10825  (0) 2022.03.23
백준 11650  (0) 2022.03.22
백준 2751  (0) 2022.03.22
백준 9465  (0) 2022.03.22