-
Notifications
You must be signed in to change notification settings - Fork 0
/
4358(Hashmap).java
46 lines (42 loc) · 1.28 KB
/
4358(Hashmap).java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class dic implements Comparable<String>{
String name;
public dic(String name){
this.name = name;
}
@Override
public int compareTo(String s){
return name.compareTo(s);
}
}
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
HashMap<String , Integer> hashMap = new HashMap<>();
int count = 0;
while (true){
String s = br.readLine();
if(s == null){
break;
}
if(!hashMap.containsKey(s)){
hashMap.put(s, 1);
}else{
hashMap.put(s, hashMap.get(s) + 1);
}
count++;
}
ArrayList<String> arrayList = new ArrayList<>(hashMap.keySet());
Collections.sort(arrayList);
for(String name : arrayList){
int num = hashMap.get(name);
String percent = String.format("%.4f", num/(double)count * 100);
sb.append(name).append(" ").append(percent).append('\n');
}
System.out.println(sb);
}
}