HDU-OJ 1004 - Let the Balloon Rise(java版)记录贴

本来是打算刷SCUOJ的,结果发现SCUOJ的题难度偏高,不适合我这种菜鸡去刷,于是还是决定刷杭电的OJ,本来是一道简单的题,结果今天脑子不太好使,犯了很多低级错误,应该吸取教训,以下是题目原题:

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

以下是我的java源码:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String input = new String();
		String color[] = new String[1000];
		int num[] = new int[1000];
		boolean flag = true;
		int count = in.nextInt();
		while(count != 0) {
			//颜色的种类数
			int k = 0;
			int adress = 0;
			for(int i = 0;i < count;i++) {
				input = in.next();
				if(k != 0) {
					for(int j = 0;j < k;j++) {
						if(input.equals(color[j])) {
							flag = false;
							num[j]++;
						}
					}
				}
				if (flag) {
					color[k] = input;
					k++;
				}
				flag = true;
			}
			int max = 0;
			for(int i = 0;i < k;i++) {
				if(num[i] > max) {
					adress = i;
					max = num[i];
				}
			}
			//对储存次数的数组进行初始化
			Arrays.fill(num , 0);
			System.out.println(color[adress]);
			//对颜色数组进行初始化
			Arrays.fill(color, " ");
			count = in.nextInt();
		}
		in.close();
	}
}

总结一下犯的错误,希望能改正我的坏毛病:
0x00:看错题目,没能理解题意。
0x01:没能及时初始化所进行记录数据的数组。
0x02:在获取int数组最大值的时候忘记将数组中最大值替换到max里。
0x03:在对scanner类的next和nextLine的选择应用中避免读入单个回车符。

大概就是以上了,希望以后做题能多加小心,不要粗心大意,加油!

你可能感兴趣的:(笔记)