HDOJ1004 Let the Balloon Rise

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 130311    Accepted Submission(s): 51457


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
  题目要求输出出现次数最多的字符串
import java.util.Scanner;

public class Main{
	private static Scanner scanner;
	public static void main(String[] args) {
		scanner = new Scanner(System.in);
		while(scanner.hasNext()){
			int n  = scanner.nextInt();
			if(n == 0){
				break;
			}
			
			String strings[] = new String[n];
			for (int i = 0; i < strings.length; i++) {
				strings[i] = scanner.next();
			}
			int p = 0;//记录出现次数最多的字符串的位置
			int max = 0;//出现的次数的最大值
			for (int i = 0; i < strings.length-1; i++) {
				int count = 0;
				for (int j = i+1; j < strings.length; j++) {
					if(strings[i].equals(strings[j])){
						count++;
					}
				}
				if(count>max){
					p = i;
					max = count;
				}
			}
			System.out.println(strings[p]);
		}
	}
}


你可能感兴趣的:(杭电ACM)