상세 컨텐츠

본문 제목

백준#10814 - 나이순 정렬

C#/알고리즘

by McRobbin 2020. 4. 20. 12:56

본문

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

 

10814번: 나이순 정렬

온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을 작성하시오.

www.acmicpc.net

정렬로 분류된 10814 - 나이순 정렬 문제 입니다.

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10814
{
    class Program
    {
        public class Register : IComparable
        {
            public static int Indexer;
 
            public int Index;
            public int Age;
            public string Name;
 
            public Register(string name, int age)
            {
                this.Age = age;
                this.Name = name;
                this.Index = Register.Indexer++;
            }
 
            public int CompareTo(Object obj)
            {
                Register other = obj as Register;
                if (this.Age == other.Age)
                    return this.Index.CompareTo(other.Index);
 
                return this.Age.CompareTo(other.Age);
 
            }
        }
 
        static void Main(string[] args)
        {
            int count = int.Parse(Console.ReadLine());
            Register.Indexer = 0;
            var RegisterList = new List<Register>();
 
            for (int i = 0; i < count; i++
            {
                string[] strRegister = Console.ReadLine().Split(' ');
                RegisterList.Add(new Register(strRegister[1], int.Parse(strRegister[0])));
            }
 
            RegisterList.Sort();
 
            foreach (var r in RegisterList)
                Console.WriteLine("{0} {1}", r.Age, r.Name);
        }
    }
}
 
 
 

 

Register라는 클래스 정의 후 IComparable을 상속받아 CompareTo를 구현했습니다.

Sort후 출력 했습니다. 먼저 온 순서를 알기 위해 static int Indexer를 뒀습니다.

 

Sort방법

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# > 알고리즘' 카테고리의 다른 글

백준#11004 - K번째 수  (0) 2020.04.20
백준#11651 - 좌표 정렬하기 2  (0) 2020.04.20
백준#11650 - 좌표 정렬하기  (0) 2020.04.20
백준#1181 - 단어 정렬  (0) 2020.04.20
백준#1026 - 보물  (0) 2020.04.20

관련글 더보기