sm 기술 블로그

114. 9375 (패션왕 신해빈) 본문

문제/백준_자바

114. 9375 (패션왕 신해빈)

sm_hope 2022. 6. 27. 20:24
import java.util.*;
import java.io.*;

class Main {
	public static void main(String args[]) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		int T = Integer.parseInt(br.readLine());

		while (T != 0) {
			HashMap<String, Integer> clothesType = new HashMap<>();
			int n = Integer.parseInt(br.readLine());

			while (n != 0) {
				String[] sBits = br.readLine().split(" ");

				if (clothesType.get(sBits[1]) != null) {
					clothesType.replace(sBits[1], clothesType.get(sBits[1]) + 1);
				} else {
					clothesType.put(sBits[1], 1);
				}
				n--;
			}

			int tmp = 1;

			for (String val : clothesType.keySet()) {
				tmp *= (clothesType.get(val) + 1);
			}
			
			sb.append(tmp-1 + "\n");
			T--;
		}
		
		System.out.print(sb);
	}
}

문제요약

옷을 입는 경우의 수를 구하라.

설명

만약 6개의 옷이 있다고 하자.

먼저 옷의 종류의 경우의 수를 구해보자.

 

head 3가지 , eyewear 3가지, face 3가지의 경우의 수가 나온다.

이제 이 모든 경우의 수를 곱해주면 된다.

 

곱해주는 이유는 1번 head 를 고르고, 1번 eyewear를 고를 경우, 2번 eyewear를 고를 경우, 없음을 고를경우 총 3가지 이고 head는 3가지의 경우를 지녔으므로 위를 3번 반복한다.

 

그러면 3 * 3 * 3 = 27이 나오고 마지막에 없음만 고르는 경우(알몸인 상태)는 빼주어야 하므로 -1을 해주면 26이 나온다.

		while (n != 0) {
			String[] sBits = br.readLine().split(" ");

			if (clothesType.get(sBits[1]) != null) {
					clothesType.replace(sBits[1], clothesType.get(sBits[1]) + 1);
			} else {
					clothesType.put(sBits[1], 1);
			}
			n--;
		}

입력형식은 

hat headgear

다음과 같다.

우리가 필요한 정보는 headgear만 필요하기 때문에 headgear만을 가지고 map을 만들 것이다.

 

만약 headgear가 있다면 map 값이 증가하고 아니면 1로 셋팅해준다.

위에서 설명한 입력값이 들어오면

다음과 같이 정리된다.

 

		for (String val : clothesType.keySet()) {
				tmp *= (clothesType.get(val) + 1);
			}
			
			sb.append(tmp-1 + "\n");
			T--;

옷의 종류에서 없음을 더해 각 옷별로 최종 경우의 수를 구하고 곱해 준 것이다.

 

마지막으로 없음만 고른상태(알몸인 상태)를 빼준 값을 Stringbuilder에 저장한다.

 

'문제 > 백준_자바' 카테고리의 다른 글

116. 2004(조합 0의 개수)  (0) 2022.06.27
115. 1676(팩토리얼 0의 개수)  (0) 2022.06.27
113. 1010 (다리놓기)  (0) 2022.06.26
112. 11051(이항 계수 2)  (0) 2022.06.26
111. 11050 (이항계수 1)  (0) 2022.06.26
Comments