첫째 줄에 가장 많이 팔린 책의 제목을 출력한다.
만약 가장 많이 팔린 책이 여러 개일 경우에는 사전 순으로 가장 앞서는 제목을 출력한다.
count랑 temp값을 만들어서 중복값 중에 가장 많은 갯수를 가진 값을 찾아보는 방법으로 해보다가
LINQ를 이용하는 방법을 찾아봄
var group = bookList.GroupBy(x => x)
.Select(g => new { Value = g.Key, Count = g.Count() })
.OrderBy(x => x.Value);
LINQ를 사용하면 데이터들을 group by를 이용해서 두 그룹으로 나눌 수 있는데 (group by into)
LINQ의 확장메서드를 이용해서 바로 group by하고 그 값을 처리할 수 있다고 한다.
select는 검색된 값을 추출하는 역할을 한다.
select안에서 익명형식으로 변수를 만들고 값을 추출할 수 있다.
orderby는 value값을 오름차순으로 정렬한다.
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
|
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace _1302
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
int inputLength = int.Parse(input);
string[] bookList = new string[inputLength];
for (int i = 0; i < bookList.Length; i++)
{
bookList[i] = Console.ReadLine();
}
var group = bookList.GroupBy(x => x)
.Select(g => new { Value = g.Key, Count = g.Count() })
.OrderBy(x => x.Value);
int maxCount = 0;
string top = null;
foreach(var x in group)
{
if(maxCount<x.Count)
{
maxCount = x.Count;
top = x.Value;
}
}
Console.WriteLine(top);
}
}
}
|
'C# > 알고리즘' 카테고리의 다른 글
11. 2504번 괄호의 값 (해결) (0) | 2020.06.22 |
---|---|
10. 10845번 큐 (해결) (0) | 2020.06.21 |
8. 1343번 폴리오미노 (0) | 2020.06.01 |
7. 1181번 단어정렬(수정) (0) | 2020.05.24 |
6. 2884번 알람시계 (0) | 2020.04.26 |