본문 바로가기
[ 다먹살 ]/- Coding

[백준] 20291 파일 정리

by 엉망으로살기 2022. 4. 8.
반응형

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

 

20291번: 파일 정리

친구로부터 노트북을 중고로 산 스브러스는 노트북을 켜자마자 경악할 수밖에 없었다. 바탕화면에 온갖 파일들이 정리도 안 된 채 가득했기 때문이다. 그리고 화면의 구석에서 친구의 메시지를

www.acmicpc.net

결국 입력값들은 "파일 이름.확장자"의 구조로 구성이 되기 때문에 '.'를 기준으로 뒤에 있는 값들만 취하면 된다. 그리고 정렬 같은 경우에는 미리 TreeMap 구조를 선언해서 map에 넣을때마다 자동으로 정렬하기 해주면 되는 간단한 문제였다.


문제 및 입출력


코드

import java.util.Scanner;
import java.util.TreeMap;
import java.util.Map.Entry;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String buf = sc.nextLine();
        // TreeMap을 이용한 key 값 기준 자동정렬
        TreeMap<String, Integer> map = new TreeMap<String, Integer>();
        
        for(int i=0; i<n; i++)
        {
            String temp = sc.nextLine();
            temp = temp.substring(temp.indexOf(".")+1);
            
            if(map.containsKey(temp))
            {
                map.put(temp, map.get(temp)+1);
            }
            else
            {
                map.put(temp, 1);
            }
        }
        
        // Map의 값을 차례대로 빼주면서 결과값 출력
        while(!map.isEmpty())
        {
            Entry<String, Integer> temp = map.pollFirstEntry();
            System.out.println(temp.getKey() + " " + temp.getValue());
        }
                
        sc.close();
    }
}

 

반응형

댓글