문제(출처: https://www.acmicpc.net/problem/5263)
< samba >
문제 풀이
HashMap을 사용하여 각 그룹의 인원이 몇 명인지 구한 후 k 명씩 줄을 세웠을 때 배치할 수 없는 그룹의 ID를 구한다.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class _5263_ { // samba
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
int num = Integer.parseInt(bf.readLine());
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
}
for (int num : map.keySet()) {
if (map.get(num) % k != 0) {
System.out.println(num);
break;
}
}
}
}
변수)
n, k : 댄서 수, 한 줄 배치 인원
map : HashMap <Integer, Integer>
num : 해당 학교 ID
댄서 수 n과 한 줄에 배치 가능한 인원 k를 입력받는다. n만큼 댄서의 학교 ID를 입력받아 HashMap에 저장하여 각 학교별 댄서 수를 구한다. HashMap을 순환하며 k로 나누어 떨어지지 않는 학교의 ID를 찾아 출력한다.
'🌞Algorithm > 🔥Baekjoon' 카테고리의 다른 글
[Baekjoon] 17048_Jarvis (1) | 2025.04.11 |
---|---|
[Baekjoon] 12005_Diamond Collector (Bronze) (0) | 2025.04.07 |
[Baekjoon] 6138_Exploration (0) | 2025.04.04 |
[Baekjoon] 15237_Cipher (0) | 2025.04.03 |
[Baekjoon] 3211_kino (0) | 2025.04.02 |