작성일 :

문제 링크

11650번 - 좌표 정렬하기

설명

입력으로 주어지는 2차원 평면 위의 점들을 정렬하는 문제입니다.

정렬의 규칙은 다음과 같습니다.

  • x 좌표가 증가하는 순으로 정렬
  • x 좌표가 같다면 y 좌표가 증가하는 순으로 정렬



Code

[ C# ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace Solution {
  using System.Text;
  class Program {
    static void Main(string[] args) {

      var n = int.Parse(Console.ReadLine()!);

      var points = new Tuple<int, int>[n];
      for (int i = 0; i < n; i++) {
        var input = Console.ReadLine()!.Split(' ');
        points[i] = Tuple.Create(int.Parse(input[0]), int.Parse(input[1]));
      }

      Array.Sort(points);

      var sb = new StringBuilder();

      foreach (var point in points)
        sb.AppendLine($"{point.Item1} {point.Item2}");
      Console.Write(sb.ToString());

    }
  }
}



[ C++ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;

  vector<pii> points(n);
  for (int i = 0; i < n; i++)
    cin >> points[i].first >> points[i].second;

  sort(points.begin(), points.end());

  for (const auto& point : points)
    cout << point.first << " " << point.second << "\n";

  return 0;
}