상세 컨텐츠

본문 제목

백준 11650번

백준 알고리즘 풀이

by 발발개발 2021. 1. 7. 18:26

본문

원본 : www.acmicpc.net/problem/11650

 

11650번: 좌표 정렬하기

첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.

www.acmicpc.net

 

풀이

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Comparator;

public class Main {
    
    public static class num {
        int a;
        int b;
        
        public num(int a, int b) {
            this.a = a;
            this.b = b;
        }
        
    }

	public static void main(String[] args) throws Exception {

		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
		
        ArrayList<num> list = new ArrayList<num>();
        int n = Integer.parseInt(reader.readLine());
        
        for (int i = 0; i < n; i++) {
            String str = reader.readLine();
            int a = Integer.parseInt(str.split(" ")[0]);
            int b = Integer.parseInt(str.split(" ")[1]);
            
            list.add(new num(a , b));
        }
        
        list.sort(new Comparator<num>() {
            @Override
       public int compare(num num1, num num2) {
           if (num1.a == num2.a) {
               return Integer.valueOf(num1.b).compareTo(Integer.valueOf(num2.b));
           } else {
               return Integer.valueOf(num1.a).compareTo(Integer.valueOf(num2.a));
           }
              
       }
});
        
		 
        for (int i = 0; i < list.size(); i++) {
            writer.write(list.get(i).a + " " + list.get(i).b + "\n");
        }
        
		writer.flush();
    }
    
}

'백준 알고리즘 풀이' 카테고리의 다른 글

백준 1181번  (0) 2021.01.08
백준 11651번  (0) 2021.01.07
백준 1427번  (0) 2021.01.07
백준 2108번  (0) 2021.01.07
백준 10989번  (0) 2021.01.05

관련글 더보기

댓글 영역