原文链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004
他的意思就是统计出现次数最多的颜色
直接用一个Map就可以AC
代码如下:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
Map<String, Integer> all = new HashMap<>();
if (n == 0) break;
for (int i = 0; i < n; i++) {
String string = sc.next();
if (all.get(string) == null) {
all.put(string,1);
} else {
all.put(string, all.get(string) + 1);
}
}
int max = 0;
String maxString = "";
for (String s: all.keySet()) {
if (all.get(s) > max) {
max = all.get(s);
maxString = s;
}
}
System.out.println(maxString);
}
}
}