-
[BOJ - Silver II] 생태학 / ⭕Algorithm/BOJ 2024. 8. 19. 20:55
1. 제출 코드 (15분 21초 / 자료구조(Map))
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Map<String, Integer> map = new HashMap<>(); String input; int cnt = 0; while(sc.hasNext()) { input = sc.nextLine(); map.put(input, map.getOrDefault(input, 0) + 1); cnt++; } List<String> keySet = new ArrayList<>(map.keySet()); Collections.sort(keySet); for(String key : keySet) { System.out.printf("%s %.4f\n", key, ((double) map.get(key) / (double) cnt) * 100); } } }
2. 구현 로직
- 해시맵을 생성하고 String, Integer로 입력받은 것을 저장
- Key만 가져와서 정렬
- 해시맵에 있는 Value와 cnt를 이용해서 정답 출력
3. 유의할 점
- BufferedReader를 이용해서 하니까 입력해서 NullPointException이랑 noSuchElement 에러가 발생한다.
- Scanner를 이용해서 입력을 받아야 한다!
'Algorithm > BOJ' 카테고리의 다른 글
[BOJ - Silver V] 피카츄 / ❌ (0) 2024.08.21 [BOJ - Silver V] 그룹 단어 체커 / ⭕ (0) 2024.08.20 [BOJ - Gold IV] 녹색 옷 입은 애가 젤다지? / ⭕ (0) 2024.08.19 [BOJ - Gold IV] 운동 / ⭕ (0) 2024.08.19 [BOJ - Gold IV] 곡예 비행 / ⭕ (0) 2024.08.18