
9375번: 패션왕 신해빈
첫 번째 테스트 케이스는 headgear에 해당하는 의상이 hat, turban이며 eyewear에 해당하는 의상이 sunglasses이므로 (hat), (turban), (sunglasses), (hat,sunglasses), (turban,sunglasses)로 총 5가지 이다.
www.acmicpc.net
해시맵을 사용하여서 key값으로는 옷의 종류(headgear, eyewear, face)가 들어가고, value로는 종류별로 옷의 개수를 넣는다.
첫번째 예제의 경우 headgear가 2개, eyewear가 1개이므로
(key = headgear, value = 2), (key = eyewear, value = 1) 이 해시맵에 들어간다.
옷을 입는 조합은 알몸인 경우만 빼면 된다.
그러므로 (각 종류별로 옷의 개수 + 1)를 모두 곱한 다음에 마지막에 알몸인 경우 한가지를 뺀다.
headgear가 2개이므로 2개에다가 1을 더한 3과,
eyewear가 1개이므로 1개에다가 1을 더한 2를 곱한 다음(3 x 2 = 6)
마지막에 알몸인 경우 1을 뺀다.
답 : (2 + 1) * (1 + 1) - 1 = 5
소스코드 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class n09375 { | |
public static void main(String[] args) throws Exception { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | |
int T = Integer.parseInt(br.readLine()); | |
for (int t = 0; t < T; t++) { | |
int n = Integer.parseInt(br.readLine()); | |
HashMap<String, Integer> map = new HashMap<>(); | |
for (int i = 0; i < n; i++) { | |
String[] sarr = br.readLine().split(" "); | |
map.computeIfAbsent(sarr[1], key -> new Integer(0)); | |
map.put(sarr[1], map.get(sarr[1]) + 1); | |
} | |
int cnt = 1; | |
for (String name : map.keySet()) { | |
cnt *= (map.get(name) + 1); | |
} | |
cnt -= 1; | |
bw.write(cnt + "\n"); | |
} | |
bw.flush(); | |
} | |
} |
'BOJ' 카테고리의 다른 글
[백준 11497번] 통나무 건너뛰기 (java) (0) | 2021.03.04 |
---|---|
[백준 2234번] 성곽 (java) (0) | 2021.03.04 |
[백준 2467번] 용액 (java) (0) | 2021.03.03 |
[백준 2660번] 회장뽑기 (java) (0) | 2021.03.02 |
[백준 4358번] 생태학 (java) (0) | 2021.03.02 |