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.

code

import java.util.Scanner;

public class Main {
    private static Scanner in;

    public static void main(String[] args) {
        in = new Scanner(System.in);
        while(in.hasNext()) {
            int a = in.nextInt();
            int max = 0,temp;
            int col = 0;
            if(a == 0) {
                break;
            }
            String[] x = new String[a];
            for(int i = 0; i < a; i++) {
                x[i] = in.next();
            }
            for(int j = 0; j < a; j++)
            {
                temp = 0;
                for(int i = 0; i < a; i++) {
                    if(x[j].equals(x[i])) {
                        temp++;
                    }
                }
                if(temp > max) {
                    max =temp;
                    col = j;
                }
            }
            System.out.println(x[col]);
        }
    }
}

tips

这题直接用暴力方法就ac了,不过发现一个问题,nextInt方法后面如果直接接上nextLine就会报错,原因是nextInt是读取分隔符前的数字,例如我输入1然后回车,nextInt会把1读取走了,但是回车没人要,就被第一个nextLine要了。解决方法是如果读取字符串就用next就行了,或者nextInt后直接加一个nextLine(),让他读取掉那个回车,再使用nextLine来进行你的程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值