[백준] 20291 - 파일 정리
1. 문제
2. 개요
N개의 파일의 파일명이 주어진다.
주어진 파일들에 대하여 사전순으로 확장자명과 해당 확장자인 파일의 개수를 출력하는 문제.
3. 풀이 및 코드
3-1. 풀이
파일명이 주어질때마다 Split()을 이용하여 점 뒤의 문자열을 따로 분리한다.
분리된 문자열은 확장자로, 등장할 때마다 딕셔너리의 해당 키의 밸류 값을 1만큼 증가시킨다.
모든 파일에 대해 작업을 완료한 후, 딕셔너리의 키 값을 사전순으로 정렬한 후 순차적으로 탐색하며 해당 밸류 값을 출력한다.
3-2. Python
import sys
N = int(sys.stdin.readline())
exdict = {}
for i in range(N):
filename = sys.stdin.readline().rstrip().split('.')
ext = filename[1]
if ext in exdict:
exdict[ext] += 1
else:
exdict[ext] = 1
for ex in sorted(exdict.keys()):
print("%s %d" %(ex, exdict[ex]))
3-3. C#
namespace boj_20291
{
internal class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
int N = int.Parse(sr.ReadLine());
Dictionary<string, int> extdict = new();
for (int i = 0; i < N; i++)
{
string[] input = sr.ReadLine().TrimEnd().Split('.');
if (extdict.ContainsKey(input[1]))
extdict[input[1]]++;
else
extdict[input[1]] = 1;
}
foreach (string ex in extdict.Keys.ToArray().OrderBy(x => x))
sw.WriteLine($"{ex} {extdict[ex]}");
sw.Close();
}
}
}
Comments