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;
}
技进乎艺,艺进乎道
原文地址:https://www.cnblogs.com/weekend/p/5464450.html