ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • section6.7
    알고리즘 2025. 8. 26. 22:30
    좌표 정렬
    N개의 평면상의 좌표(x, y)가 주어지면 모든 좌표를 오름차순으로 정렬하는 프로그램을 작성하세요.
    정렬기준은 먼저 x값의 의해서 정렬하고, x값이 같을 경우 y값에 의해 정렬합니다.
    
    입력설명
    첫째 줄에 좌표의 개수인 N(3<=N<=100,000)이 주어집니다.
    두 번째 줄부터 N개의 좌표가 x, y 순으로 주어집니다. x, y값은 양수만 입력됩니다.
    
    출력설명
    N개의 좌표를 정렬하여 출력하세요.
    입력예제 1
    5
    2 7
    1 3
    1 2
    2 5
    3 6
    
    출력예제 1
    1 2
    1 3
    2 5
    2 7
    3 6

     

     

     

    ANSWER

    public class Question7 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < n; i++) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                map.put(x, y);
            }
    
            solution(n, map);
        }
    
        public static ArrayList<Map<Integer, Integer>> solution(int n, Map<Integer, Integer> map) {
            ArrayList<Map<Integer, Integer>> answer = new ArrayList<>();
            int[] arr = new int[n];
    
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                // System.out.println("[key]:" + entry.getKey() + ", [value]:" + entry.getValue());
    
            }
            return answer;
        }
    }

     

     

     

    SOLUTION

    public class Answer7 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            ArrayList<Point> arr = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                arr.add(new Point(x, y));
            }
            // 정렬, 정렬의 기준이 compareTo
            Collections.sort(arr);
    
            for (Point o : arr) {
                System.out.print(o.x + " " + o.y);
            }
        }
    }
    
    class Point implements Comparable<Point> {
        public int x, y;
        Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        @Override
        public int compareTo(Point o) {
            // 오름차순으로 정렬하고 싶으면 음수가 return
            // 내림차순으로 정렬하고 싶으면 양수가 return
            if (this.x == o.x) {
                return this.y - o.y;
            } else {
                return this.x - o.x;
            }
        }
    }
    728x90

    '알고리즘' 카테고리의 다른 글

    section6.9  (1) 2025.08.26
    section6.8  (0) 2025.08.26
    section6.6  (0) 2025.08.21
    section6.5  (0) 2025.08.21
    section6.4  (0) 2025.08.20
Designed by Tistory.