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稍微纪念一下自己第一篇博客-