티스토리 뷰

반응형

문제

해빈이는 패션에 매우 민감해서 한번 입었던 옷들의 조합을 절대 다시 입지 않는다. 예를 들어 오늘 해빈이가 안경, 코트, 상의, 신발을 입었다면, 다음날은 바지를 추가로 입거나 안경대신 렌즈를 착용하거나 해야한다. 해빈이가 가진 의상들이 주어졌을때 과연 해빈이는 알몸이 아닌 상태로 며칠동안 밖에 돌아다닐 수 있을까?

입력

첫째 줄에 테스트 케이스가 주어진다. 테스트 케이스는 최대 100이다.

  • 각 테스트 케이스의 첫째 줄에는 해빈이가 가진 의상의 수 n(0 ≤ n ≤ 30)이 주어진다.
  • 다음 n개에는 해빈이가 가진 의상의 이름과 의상의 종류가 공백으로 구분되어 주어진다. 같은 종류의 의상은 하나만 입을 수 있다.

모든 문자열은 1이상 20이하의 알파벳 소문자로 이루어져있으며 같은 이름을 가진 의상은 존재하지 않는다.

출력

각 테스트 케이스에 대해 해빈이가 알몸이 아닌 상태로 의상을 입을 수 있는 경우를 출력하시오.

 

Java Solution

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        PassionKing passionKing = new PassionKing();
        Scanner sc = new Scanner(System.in);
        int inputCountTC = sc.nextInt();

        for (int i = 0; i < inputCountTC; i++) {
            int inputCountCloths = sc.nextInt();

            for (int j = 0; j < inputCountCloths; j++) {

                String clothName = sc.next();
                String clothType = sc.next();

                passionKing.addNumOfCloths(clothType);
            }
            int result = passionKing.multipleEachClothTypes();
            System.out.println(result);
            passionKing.initHashMap();
        }
    }
}

class PassionKing {
    private static final int baseNumCloth = 1;
    private static final int naked = 1;

    private Map<String, Integer> clothsList = new HashMap<>();

    void addNumOfCloths(String clothType) {
        if (checkContainsKey(clothType)) {
            int numOfCloths = clothsList.get(clothType);
            clothsList.put(clothType, numOfCloths + baseNumCloth);
        }
        if (!checkContainsKey(clothType)) {
            clothsList.put(clothType, baseNumCloth);
        }
    }

    private boolean checkContainsKey(String clothType) {
        return clothsList.containsKey(clothType);
    }

    void initHashMap() {
        clothsList.clear();
    }


    int multipleEachClothTypes() {
        int result = baseNumCloth;
        for (int cloth : clothsList.values()) {
            result *= cloth + baseNumCloth;
        }
        return result - naked;
    }

}

 

기본 Scanner 사용시

Q. Scanner 대신 BufferReader와 StringTokenizer을 써보면?

 

 

 

 

 

구분자가 생략되면 구분자는 기본적으로 공백(space)가 된다.

StringTokenizer st 

= new StringTokenizer("문자열""구분자");

 

 

 

 

 

 

 

 

 

 

Q. Scanner 대신 BufferReader의 split을 사용한다면?

 

 

 

 

 

 

 

 

 

원하는 배열 요소에 직접 접근하여 값을 가져온다.

 

 

 

 

 

 

 

반응형

'Algorithms > BOJ' 카테고리의 다른 글

백준 Q.1157 단어 공부  (0) 2020.01.08
백준 Q.1159 농구 경기  (0) 2020.01.08
백준 Q.1302 베스트셀러  (0) 2020.01.07
백준 Q.10814 나이 순 정렬하기  (0) 2020.01.03
백준 Q.11650 좌표 정렬하기  (0) 2020.01.03
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함