https://www.acmicpc.net/problem/4358
4358번: 생태학
프로그램은 여러 줄로 이루어져 있으며, 한 줄에 하나의 나무 종 이름이 주어진다. 어떤 종 이름도 30글자를 넘지 않으며, 입력에는 최대 10,000개의 종이 주어지고 최대 1,000,000그루의 나무가 주어
www.acmicpc.net
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.PriorityQueue;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static double cnt;
public static void main(String[] args) throws Exception {
HashMap<String, Integer> map = new HashMap<>();
while (true) {
String input = br.readLine();
if (input == null || input.isEmpty()) break;
if (map.containsKey(input)) map.put(input, map.get(input) + 1);
else map.put(input, 1);
cnt++;
}
PriorityQueue<Tree> pq = new PriorityQueue<>();
map.entrySet().forEach(x -> {
pq.offer(new Tree(x.getKey(), x.getValue() / cnt * 100));
});
while(!pq.isEmpty()) {
Tree t = pq.poll();
sb.append(String.format("%s %.4f", t.name, t.cnt)).append("\n");
}
System.out.print(sb.toString());
}
static class Tree implements Comparable<Tree> {
String name;
double cnt;
public Tree (String name, double cnt) {
this.name = name;
this.cnt = cnt;
}
@Override
public int compareTo (Tree o) {
return this.name.compareTo(o.name);
}
}
}
'OJ' 카테고리의 다른 글
[BOJ] 1254 팰린드롬 만들기 (JAVA) (0) | 2023.02.20 |
---|---|
[BOJ] 10820 문자열 분석 (JAVA) (0) | 2023.02.19 |
[BOJ] 1032 명령 프롬프트 (JAVA) (0) | 2023.02.17 |
[BOJ] 5635 생일 (JAVA) (0) | 2023.02.16 |
[BOJ] 1969 DNA (JAVA) (0) | 2023.02.15 |