반응형
SMALL
듣잡과 보잡들중 듣보잡 2관왕을 가려내는 문제
문제풀이
using System;
using System.Collections.Generic;
namespace BOJ00
{
class Program
{
static void Main(string[] args)
{
//듣잡 보잡 배열
string[] whoAreYouList = { "ohhenrie", "charlie", "baesangwook", "obama", "baesangwook", "ohhenrie", "clinton" };
//듣보잡들의 숫자를 셀 리스트
Dictionary<string, int> nuguList = new Dictionary<string, int>();
//듣잡 보잡들을 리스트에 담고 누가 듣보잡인지 세기 (2면 듣보잡, 1이면 듣잡 아님 보잡)
foreach (string nobody in whoAreYouList)
{
int temp = 0;
if (nuguList.ContainsKey(nobody))
nuguList[nobody]++;
else nuguList.Add(nobody, temp + 1);
}
//최종적으로 듣보잡을 가려낼 리스트 생성
List<string> nodieaList = new List<string>();
foreach (var noIdea in nuguList)
{ //리스트를 돌며 듣잡 보잡들중 2관왕을 찾는다
if (noIdea.Value == 2)
nodieaList.Add(noIdea.Key);
}
//듣보잡들의 집합의 요소수를 구한다.
Console.WriteLine(nodieaList.Count);
//듣보잡들을 오름차순으로 정렬해주고
nodieaList.Sort();
//듣보잡들을 출력한다.
nodieaList.ForEach(delegate (string name)
{
Console.WriteLine(name);
});
}
}
}
결과창
아까 해결한 결승선 통과 문제와 같이 Dictionary<T> 를 이용하니 쉽게 해결할수 있었다.
각 항목별 중복되는 이름은 없으니 더욱 쉽게 해결 가능했다.
반응형
LIST
'Algorithm > BOJ' 카테고리의 다른 글
[BOJ] 5585 거스름돈 C# 풀이 (0) | 2023.01.20 |
---|---|
[BOJ] 1874 스택 수열 C# 풀이 (0) | 2023.01.20 |
[프로그래머스] 완주하지 못한 선수 C# 풀이 (1) | 2023.01.19 |
[BOJ] 2979 트럭 주차 C# 풀이 (0) | 2023.01.19 |
[BOJ] 10808 알파벳 개수 C# 풀이 (0) | 2023.01.19 |