杭电oj-1004(Let the Balloon Rise)

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

Author

WU, Jiazhi

刚开始读这道题的时候,真的有点读乱啦,然后就各种翻译,后来才明白什么意思~其实就是说,我们会输入一堆字符串,让我们调出来里面出现次数最多的那个字符串。这道题我采用的是键值对的方式,当然在java里面我们有现成的方式实现它那就是hashMap,当然在很多其它的语言中,大家都亲切的称他为字典,原理是一样的,我就是遇到一个数,就看看字典里面有没有,如果有那就把这个键对应的值加一再放回去,如果没有那就把这个值当做键,把数字1当做值放进去,最后遍历一下我们的字典,找出值最大的那个,记下值最大的数的键,最终输出这个键就好了。

代码:


import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;

import java.util.Scanner;

public class Main1004 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        List<String> list = new ArrayList<>();
        while (true) {
            Map<String, Integer> aMap = new HashMap<>();
            int T = in.nextInt();
            if (T != 0) {
                int m = T;
                while (T > 0) {
                    String bString = in.next();
                    if (aMap.get(bString) == null) {
                        aMap.put(bString, 1);
                    } else {
                        int count = aMap.get(bString);
                        aMap.remove(bString);
                        count++;
                        aMap.put(bString, count);
                    }
                    T--;
                }
                int i = 0;
                String result = null;
                for (String key : aMap.keySet()) {
                    int a = aMap.get(key);
                    if (a > i) {
                        i = a;
                        result = key;
                    }
                }
                list.add(result);
            } else {
                for (int i = 0; i < list.size(); i++) {
                    System.out.println(list.get(i));
                }

                return;
            }

        }

    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,268评论 19 139
  • 庚辰、辛巳两年,余于成都茶店子育才路二十中旁经营“博士屋”礼品店,端木赐之才乏,意则不中,陶朱公之智短,徒此虚亏。...
    李野航阅读 678评论 0 1
  • 众所周知在云南除过桥米线外,鲜花饼也是本地特色小吃之一,现在我想聊聊我大云南的鲜花饼。 鲜花饼,这三个字简单而易懂...
    七月hope阅读 940评论 0 1
  • 在忙碌的一段时间之后,我的世界依然跟ppt紧密联系,有时候觉得自己为什么会频繁的接触方案,从初来乍到再到现在对pp...
    lilinjuan阅读 182评论 0 4
  • 【快乐-勤奋-感恩】(12.27) 一、【时间】: 两个月(20161227~20170227) 二、【目标】: ...
    夏苗苗阅读 117评论 0 0