zoukankan      html  css  js  c++  java
  • zoj 2104 Let the Balloon Rise(map映照容器的应用)

    题目链接:

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2104

    题目描述:

    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

     1 /*问题 统计每种颜色气球的个数并输出
     2 解题思路 问题本身很简单,使用结构体数组也可以做,这里提供一种map映照容器的做法,效率更高
     3 具体做法:每读入一个字符串,先搜索该键值的是否存在,存在修改映照数据,不存在插入,最后遍历找到映照数据
     4 最大即可*/ 
     5 #include <cstdio>
     6 #include <map>
     7 #include <string>
     8 using namespace std;
     9 
    10 int main()
    11 {
    12     int n;
    13     char s[20];
    14     string colo; 
    15     map<string,int> m; 
    16     map<string,int>::iterator it,max;
    17     while(scanf("%d",&n), n != 0)
    18     {
    19         while(n--)
    20         {
    21             scanf("%s",s);
    22             colo=s;
    23             it=m.find(colo); 
    24             if(it != m.end())
    25                 m[colo]++;
    26             else
    27                 m[colo]=1;
    28         }
    29         
    30         max=m.begin();
    31         for(it=m.begin(); it != m.end();it++){
    32             if(it->second > max->second)
    33             //if((*it).second > (*max).second) 
    34                 max=it;
    35         }
    36         printf(max->first.c_str());//转换 
    37         putchar('
    ');
    38         m.clear();
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    《c程序设计语言》读书笔记--大写转小写
    《c程序设计语言》读书笔记--字符串比较
    《c程序设计语言》读书笔记--反转字符串
    spring接收json字符串的两种方式
    logback的使用
    初识Vim
    Chrome控制台
    构造有层次的大纲
    让chrome浏览器快的不要不要的
    排序算法Java版
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/8543700.html
Copyright © 2011-2022 走看看