Let the Balloon Rise

Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you. 
 
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
 
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 
Sample Input
5 green red blue red red 3 pink orange pink 0
 
Sample Output
red pink
 
AC代码:
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String args[]) {
 5         Scanner sc = new Scanner(System.in);
 6         while (sc.hasNext()) {
 7             int n = sc.nextInt();
 8             String str[] = new String[n];
 9             int a[] = new int[n];     //该数组用于存放从该位置之后,一共有多少个字符串与该位置的字符串相同
10             for (int i = 0; i < n; i++) {
11                 a[i] = 0;
12             }
13             if (n == 0) {
14                 break;
15             }
16             for (int i = 0; i < n; i++) {   //str中存放ballon的值
17                 str[i] = sc.next();
18             }
19             for (int i = 0; i < n; i++) {
20                 for (int j = i + 1; j < n; j++) {
21                     if (str[i].equals(str[j])) {      //因为这里的str[]是引用/所以不能直接用==判断
22                         a[i]++;
23                     }
24                 }
25             }
26             int max = 0;
27             for (int i = 0; i < n; i++){
28                 if(a[i]>a[max]) {
29                     max = i;
30                 }
31             }
32             System.out.println(str[max]);
33         }
34     }
35 }

 

 

posted @ 2017-12-13 16:38  ixummer  阅读(197)  评论(0编辑  收藏  举报