HDOJ_1004

HDOJ_1004

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.

java AC

最近开始刷HDOJ(杭电)的算法题,比较菜,从第一个开始刷
今天做到1004,使用java发现有很多bug
首先贴代码:

package practice;
import java.util.Scanner;
public class _1004 {
	static Scanner in = new Scanner(System.in);
	
	public static void main(String[] args) {
		int n;
		String []out = new String[1001];
		for(int i = 0;i < 1001;i++) {
			out[i] = null;
			
		}
		int index = 0;
		while(in.hasNext()) {
			n = in.nextInt();

			if(n <= 0) {
				
			}
			else {
				String []str = new String[n+1];
				for(int i = 0;i <= n;i++) {
					str[i] = new String();
				}
				//输入
				for(int i = 1;i <= n;i++) {
					str[i] = in.next();
				}
				//			str[0]作为放置最受欢迎的balloon
				int max_ = 1;
				int []sum = new int[n+1];
				
				for(int i = 1;i < n;i++) {
					sum[i] = 0;
					if(str[i] == null) continue;
					
					for(int j = i+1;j <= n;j++) {
						if(str[j] == null) continue;
						
						if(str[i].compareTo(str[j]) == 0) {
							sum[i]++;
							str[j] = null;
						}
					}
				}
				int max = 0;
				for(int i = 1;i <= n;i++) {
					if(str[i] == null) continue;
					
					if(sum[i] > max) {
						max = sum[i];
						max_ = i;
						
					}
				}
				System.out.println(str[max_]);
			}
			
		}
	}
}

这个题很简单,暴力破解了(捂脸),主要是细节处很需要注意
首先,在输入的时候. next()nextLine()是有区别的,由于OJ系统的严格性,必须检查,特别是空格.
其次,在输出的时候需要使用
println()
,不能使用printf("\n").而正是由于这个原因,导致自己submit了十几次都 presentat error
虽然这个题很简单,但是细节很麻烦.
还有需要注意,当n==0时是不处理,但是不是结束输入,结束输入依然是文件结束符
OK了

emmm稍微纪念一下自己第一篇博客-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值