杭电ACM OJ 1004 Let the Balloon Rise 考察map和list的遍历
来源:互联网 发布:uml软件建模过程 编辑:程序博客网 时间:2024/06/16 07:03
Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 129323 Accepted Submission(s): 51086
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.
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.
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
5greenredblueredred3pinkorangepink0
Sample Output
redpink翻译:就是找到输入几个字符串,找到最多的那个。
核心问题:你不知道会输入什么,因此无法预定义。不过题目还是非常简单,这里采用java,简单地使用map和list就可以解决。把字符串放到list中,用循环从list中取,如果这个键(比如green)map中没有,那就给他添加。有了,map取得值加1即可。
public class LetTheBalloonRise1004 { private static List<String> list; public static void main(final String[] args) throws Exception { initData(); List<String> mostPopularList = calculate(list); for (String s : mostPopularList) { System.out.println(s); } } private static void initData() { list = initStringList("green", "red", "blue", "blue", "red"); } private static List<String> initStringList(String... s) { List<String> list = new ArrayList<>(); Collections.addAll(list, s); return list; } private static List<String> calculate(List<String> list) { Map<String, Integer> map = new HashMap<>(); for (String s : list) { if (map.get(s) == null) { map.put(s, 1); } else { int formal = map.get(s); map.put(s, ++formal); } } //遍历map int max = 0; List<String> mostPopularList = new ArrayList<>();//考虑有多个最大的情况 for (Map.Entry<String, Integer> entry : map.entrySet()) { if (entry.getValue() > max) { //如果遍历发现更大的值,清空集合,因为之前里面可能有多个最大 mostPopularList.clear(); mostPopularList.add(entry.getKey()); max = entry.getValue(); } else if (entry.getValue() == max) { //如果有个值相等的,添加到后一位即可 mostPopularList.add(entry.getKey()); } } return mostPopularList; }}
阅读全文
0 0
- 杭电ACM OJ 1004 Let the Balloon Rise 考察map和list的遍历
- 杭电OJ Let the Balloon Rise
- 杭电oj-------1004 Let the Balloon Rise
- 杭电oj 1004 Let the Balloon Rise
- Let the Balloon Rise(杭电ACM 1004)
- 杭电acm 1004 (Let the Balloon Rise)
- 杭电ACM 1004 “+Let+the+Balloon+Rise”
- 杭电ACM-1004 Let the Balloon Rise
- 杭电acm 1004 Let the Balloon Rise
- 杭电ACM----------1004 Let the Balloon Rise
- 杭电ACM 1004 Let the Balloon Rise
- 杭电ACM—1004 Let the Balloon Rise
- 杭电ACM(1004) Let The Balloon Rise
- 杭电ACM 1004 Let the Balloon Rise
- 杭电 1004[Let the Balloon Rise]
- 杭电 1004 Let the Balloon Rise
- 杭电 1004 Let the Balloon Rise
- 杭电 1004 Let the Balloon Rise
- Windows I/O操作
- Echarts(二、柱状图(各参数详细描述))
- 一、java项目常用工具类之加密工具类
- JAVA线程小结
- CodeForces
- 杭电ACM OJ 1004 Let the Balloon Rise 考察map和list的遍历
- Almost Identity Permutations CodeForces
- Python——安装编译环境
- 字母大小转换及其十进制的ASCLL码值
- 时钟
- python语法基础(一)
- 后台开发面试常被问到的问题(一):什么之间的区别
- laravel上传文件报错:413 Request Entity Too Large
- jq中 text() html() val()