题意:多组数据,每组给一个n和n行颜色单词,统计每组最受欢迎的颜色,当n=0时结束
1 import java.util.HashMap; 2 import java.util.Map; 3 import java.util.Scanner; 4 5 public class Main{ 6 public static void main(String[] args) { 7 Scanner in = new Scanner(System.in); 8 int n = 0; 9 while (in.hasNext()) { 10 n = in.nextInt(); 11 if (n == 0) { 12 break; 13 } 14 String[] color = new String[n]; 15 int[] cnt = new int[n]; 16 for (int i = 0; i < n; ++i) { 17 color[i] = in.next(); 18 } 19 for (int i = 0; i < n - 1; ++i) { 20 for (int j = i + 1; j < n; ++j) { 21 if (color[i].equals(color[j])) { 22 ++cnt[i]; 23 } 24 } 25 } 26 int idx = 0; 27 int maxx = 0; 28 for (int i = 0; i < n; ++i) { 29 if (cnt[i] > maxx) { 30 maxx = cnt[i]; 31 idx = i; 32 } 33 } 34 System.out.println(color[idx]); 35 } 36 } 37 }