zoukankan      html  css  js  c++  java
  • HDU 1004

    HDU 1004题目

    输入:

    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.

    输出:

    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.

    分析:本题使用了STL的map容器,写代码时在找出出现频率最高的字符时卡住了,其实需要两个变量,一个变量Max存储最高频率数,第二个变量MaxColor存储最高频率对应的字符。

    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    map<string ,int> cnt;//
    map<string ,int> :: iterator iter;
    
    int main(){
      int N;
      string s;
    
      while(cin>>N&&N){
        int Max=0;
        string MaxColor;
        cnt.clear();
        while(N--){
        cin>>s;
        if(!cnt.count(s)) cnt[s]=0;
        cnt[s]++;
        }
        for(iter=cnt.begin();iter!=cnt.end();iter++)
        {
            if(iter->second > Max) {Max = iter->second;MaxColor = iter->first;}
        }
        cout<<MaxColor<<endl;
      }
        return 0;
    }
    技进乎艺,艺进乎道
  • 相关阅读:
    angular 项目 error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'
    chrome 总崩溃的正确解决方法
    angular 学习日志
    mongodb 3.4 学习 (二)命令
    mongodb 3.4 学习 (一) 安装
    python中文入库
    [转贴] 流量统计脚本
    监控系统开发的一些参考
    nagios centos7 rpm打包
    collectd配置
  • 原文地址:https://www.cnblogs.com/weekend/p/5464450.html
Copyright © 2011-2022 走看看