코딩테스트/백준

[백준] 좌표 정렬하기2 11651 - JAVA

ankisile 2023. 3. 31. 22:56

문제

https://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.util.*;
import java.io.*;

public class Main {
	static class Point implements Comparable<Point>{

		int x;
		int y;
				
		public Point(int x, int y) {
			this.x = x;
			this.y = y;
		}


		@Override
		public int compareTo(Main.Point o) {
			if(y == o.y)
				return x - o.x;
			return y - o.y;

		}
		
	}
		
	public static void main(String args[]) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
	
		int n = Integer.parseInt(br.readLine());
		
		Point arr [] = new Point[n];
		
		for(int i=0;i<n;i++) {
			st = new StringTokenizer(br.readLine());
			arr[i] = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
		}
		
		Arrays.sort(arr);
		
		for (int i = 0; i < n; i++) {
			System.out.println(arr[i].x+" "+arr[i].y);
		}
	}
   
	
	
   
}