https://www.acmicpc.net/problem/15595
1. 아이디어
HashSet과 HashMap을 활용해서 해결할 수 있었다.
2. 문제풀이
Set에는 문제를 맞은 사람의 id, Map에는 문제를 틀린 사람의 id를 key, 문제를 틀린 횟수를 value에 저장했다. 문제를 틀린 횟수는 문제를 맞은 후로는 세지 않아야 한다. 입력을 받아 저장한 후 Map을 순회하며 Set에 있는 id인 경우에만 횟수를 세주면 문제를 맞은 사람이 문제를 맞기 전까지 틀린 횟수의 총합을 간단하게 구할 수 있다.
3. 코드
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
Set<String> set = new HashSet<>();
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int no = Integer.parseInt(st.nextToken());
String id = st.nextToken();
int result = Integer.parseInt(st.nextToken());
if (result == 4) {
set.add(id);
} else {
if (set.contains(id)) continue;
map.put(id, map.getOrDefault(id, 0) + 1);
}
}
set.remove("megalusion");
int sum = set.size();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (!set.contains(entry.getKey())) continue;
sum += entry.getValue();
}
if ((sum) == 0) System.out.println(0);
else System.out.println((double) set.size() / sum * 100);
}
}
4. 후기
'코딩테스트 준비 > 백준' 카테고리의 다른 글
[백준] 10026번 - 적록색약 [Java] (0) | 2025.01.23 |
---|---|
[백준] 6593번 - 상범 빌딩 [Java] (0) | 2025.01.23 |
[백준] 16200번 - 해커톤 [Java] (0) | 2025.01.22 |
[백준] 29198번 - 이번에는 C번이 문자열 [Java] (0) | 2025.01.22 |
[백준] 28094번 - 기말고사 작품 전시 [Java] (0) | 2025.01.22 |