상세 컨텐츠

본문 제목

백준#11650 - 좌표 정렬하기

C#/알고리즘

by McRobbin 2020. 4. 20. 12:20

본문

https://www.acmicpc.net/problem/11650

 

11650번: 좌표 정렬하기

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

www.acmicpc.net

정렬로 분류된 11650 좌표 정렬하기 문제 입니다.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _11650
{
    class Program
    {
        public class Pair : IComparable
        {
            public int x;
            public int y;
 
            public Pair(int x, int y)
            {
                this.x = x;
                this.y = y;
            }
 
            public int CompareTo(Object obj)
            {
                Pair other = obj as Pair;
                if (this.x == other.x)
                    return this.y.CompareTo(other.y);
 
                return this.x.CompareTo(other.x);
            }
        }
        static void Main(string[] args)
        {
            int count = int.Parse(Console.ReadLine());
            var PairList = new List<Pair>();
 
            for(int i = 0; i < count; i++)
            {
                string[] pair = Console.ReadLine().Split(' ');
                PairList.Add(new Pair(int.Parse(pair[0]), int.Parse(pair[1])));
            }
 
            PairList.Sort();
 
            foreach (Pair pair in PairList)
                Console.WriteLine("{0} {1}", pair.x, pair.y);
        }
    }
}
 
 
 

 

Pair라는 클래스 생성후 IComparable 상속받아 CompareTo 정의 했습니다. 

정렬 방법 입니다.

https://programming-mr.tistory.com/46

 

C# List Sort예제.

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69..

programming-mr.tistory.com

 

'C# > 알고리즘' 카테고리의 다른 글

백준#11651 - 좌표 정렬하기 2  (0) 2020.04.20
백준#10814 - 나이순 정렬  (0) 2020.04.20
백준#1181 - 단어 정렬  (0) 2020.04.20
백준#1026 - 보물  (0) 2020.04.20
백준#1427 - 소트인사이드  (0) 2020.04.19

관련글 더보기