하루 한 접시

[백준] 11651번: 좌표 정렬하기 2 [C#]

NaZZU 2024. 3. 23. 00:28

첫째 줄에 반복횟수가, 둘째 줄부터는 x, y좌표가 문자열 형식으로 한 줄로 입력된다.

좌표 정렬하기(1) 문제와는 조금 다르게 y좌표를 우선적으로 정렬하고, x좌표를 정렬하는 문제이다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
 
namespace 연습장
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //using var reader = new StreamReader(Console.OpenStandardInput());
            //using var print = new StreamWriter(Console.OpenStandardOutput());
            StringBuilder sb = new StringBuilder();
 
            List<Tuple<intint>> list = new List<Tuple<intint>>();
 
            int N = int.Parse(Console.ReadLine());
            string[] str;
 
            for (int i = 0; i < N; i++)
            {
                str = Console.ReadLine().Split();
                list.Add(new Tuple<intint>(int.Parse(str[1]), int.Parse(str[0])));
            }
            list.Sort();
 
            foreach (Tuple<intint> a in list)
            {
                sb.Append(a.Item2 + " " + a.Item1 + "\n");
            }
 
            Console.WriteLine(sb);
        }
    }
}
cs

뭐 대단한 알고리즘이 떠오르지 않아서 그냥 리스트에 튜플 형식으로 넣어줄 때 x좌표와 y좌표의 순서를 바꿔서 넣었고, 출력할때는 다시 반대로 x좌표, y좌표 순서대로 출력해주었다.