《ACM程序设计》书中题目 O

来源:互联网 发布:混凝土配合比软件 编辑:程序博客网 时间:2024/05/16 06:47

题意(分析):

输入一组颜色,判断哪个颜色出现最多,输出;

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,j,k,n;
    map<string,int>m;
    string s;
    map<string,int>::iteratorp1,p;
    while(cin>>n&&n!=0)
    {
        for(i=0;i<n;i++)
        { cin>>s;
            p=m.find(s);
            if(p!=m.end())(p->second)+=1;
            else m.insert(make_pair(s,1));
        }
        for(p=m.begin(),p1=m.begin();p1!=m.end();p1++)
        {
            if((p->second)<(p1->second)) p=p1;
        }
        cout<<p->first<<endl;
        m.clear();
    }
    return 0;
}

感想:

利用map,合理利用STL,多加练习,会更加熟练;

0 0