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.
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.
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
Java Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int kinds = 0,n;
int ca,cb,max;
String[] st=new String[1010]; //最多1000个输入
int[] a=new int[1010]; //输入的对应统计
String str;
n=sc.nextInt();
while(n>0){
kinds=0; //每一次开始时,种类致 0
for(ca=0; ca<n; ca++){
str=sc.next();
if(kinds==0){ //第一个输入的种类
st[0]=str;
a[0]++;
kinds++;
continue;
}
for(cb=0; cb<kinds; cb++){ //从第二次开始,每次和以前的比较,发现有没有相同的
if(st[cb].equals(str)){
a[cb]++;
break;
}
}
if(cb==kinds){ //此时,说明这是个新颜色
st[cb]=str;
a[cb]++;
kinds++;
}
}
//以下打印出现最多的颜色
max=0;
for(ca=0; ca<kinds; ca++){
if(a[ca]>a[max]){
max=ca;
}
}
System.out.println(st[max]);
//一次结束后,将统计致 0
for(ca=0; ca<kinds; ca++){
a[ca]=0;
}
n=sc.nextInt();
}
}
}