PS/BaekJoon

11650 JAVA ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ

chaerlo127 2022. 7. 12. 22:48
728x90


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 Main {

	public static void main(String[] args) {
		BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		int a;
		try {
			a = Integer.parseInt(buffer.readLine());
			int[][] xy = new int[a][2];
			
			for(int i = 0; i<a; i++) {
				StringTokenizer st = new StringTokenizer(buffer.readLine()); // ๊ณต๋ฐฑ ๋‹จ์œ„๋กœ ์ž…๋ ฅ ๋ฐ›์Œ
				int x = Integer.parseInt(st.nextToken());
				int y = Integer.parseInt(st.nextToken());
				xy[i][0] = x;
				xy[i][1] = y;
			}
			
			Arrays.sort(xy, new Comparator<int []>() {

				@Override
				public int compare(int[] o1, int[] o2) {
					if(o1[0] == o2[0]) { // ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋“ค์ด ๊ฐ™๋‹ค๋ฉด
						return o1[1] - o2[1]; // ๋‘ ๋ฒˆ์งธ ๋ฐฐ์—ด ์˜ค๋ฆ„์ฐจ์ˆœ
					}
					return o1[0] - o2[0]; // ์ฒซ ๋ฒˆ์งธ ์›์†Œ ์˜ค๋ฆ„์ฐจ์ˆœ
				}
			});
			for(int i = 0; i<a; i++) {
				sb.append(xy[i][0]).append(" ").append(xy[i][1]).append("\n");
			}
			System.out.println(sb);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}

โœจ StringTokenizer

๋ฌธ์ž์—ด์„ ์šฐ๋ฆฌ๊ฐ€ ์ง€์ •ํ•œ ๊ตฌ๋ถ„์ž๋กœ ๋ฌธ์ž์—ด์„ ์ชผ๊ฐœ์ฃผ๋Š” ํด๋ž˜์Šค

 

[์ถœ์ฒ˜ ๋ฐ ์ค‘์š” ๋ฌธ๋ฒ•] https://jhnyang.tistory.com/398

 

 

 

 

 

โœจ  Comparator

[์ถœ์ฒ˜ ๋ฐ ์ค‘์š” ๋ฌธ๋ฒ•] https://velog.io/@neity16/Java-%EA%B8%B0%EB%B3%B83-Comparable-Comparator-%EC%B0%A8%EC%9D%B4%EC%A0%90#%EC%84%A4%EB%AA%85-1

728x90