상세 컨텐츠

본문 제목

백준#11652 - 카드

C#/알고리즘

by McRobbin 2020. 4. 29. 16:31

본문

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

 

11652번: 카드

준규는 숫자 카드 N장을 가지고 있다. 숫자 카드에는 정수가 하나 적혀있는데, 적혀있는 수는 -262보다 크거나 같고, 262보다 작거나 같다. 준규가 가지고 있는 카드가 주어졌을 때, 가장 많이 가지고 있는 정수를 구하는 프로그램을 작성하시오. 만약, 가장 많이 가지고 있는 정수가 여러 가지라면, 작은 것을 출력한다.

www.acmicpc.net

정렬로 분류된 11652 카드 문제 입니다.

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _11652
{
    class Program : IComparer<long>
    {
        static void Main(string[] args)
        {
            int count = int.Parse(Console.ReadLine());
 
            var inputDict = new Dictionary<longint>();
 
            for (int i = 0; i < count; i++)
            {
                long input = long.Parse(Console.ReadLine());
                if (inputDict.ContainsKey(input))
                    inputDict[input] += 1;
                else
                    inputDict.Add(input, 1);
            }
 
            //갯수 역으로 정렬.
            inputDict = inputDict.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
            var maxEnumer = from kvp in inputDict where kvp.Value == inputDict.First().Value select kvp.Key;
 
            var maxList = maxEnumer.ToList();
            maxList.Sort(new Program());
            Console.WriteLine(maxList.First());
        }
 
        public int Compare(long l1, long l2)
        {
            return l1.CompareTo(l2);
        }
    }
}
 
 
 

 

입력 수의 길이가 길어 long으로 받았습니다.

딕셔너리에 long값을 key로, 갯수를 value로 했습니다.

 

딕셔너리의 value에 대해 정렬하고 최댓값과 같은 KeyValuePair를 뽑아 저장합니다.

그 키값을 정렬해 가장 작은것을 출력하면 되겠습니다.

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

백준#1431 - 시리얼 번호  (0) 2020.05.02
백준#5052 - 전화번호 목록  (0) 2020.05.02
백준#3047 - ABC  (0) 2020.04.21
백준#10825 - 국영수  (0) 2020.04.21
백준#11004 - K번째 수  (0) 2020.04.20

관련글 더보기