hdu_1004_let the balloon rise

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        map<string,int> mp;
        int max=0;
        string maxs;
        if(n==0){
            break;
        }
        while (n--) {
            string temp;
            cin>>temp;
            mp[temp]++;
            int temp1=mp[temp];
            if(temp1>max){
                max=temp1;
                maxs=temp;
            }
        }
        cout<<maxs<<endl;
    }
    return 0;
}

输入之后直接和最大之比较即可,下面的方法有些笨拙.

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){
    int n,i;
    while(cin>>n){
        map<string,int> mp;
        int index=0,rec[1010]={0},max=0,maxi=0;
        if(n==0){
            break;
        }
        for(i=0;i<n;i++){
            string temp;
            cin>>temp;
            if(mp.find(temp)==mp.end()){
                mp[temp]=index;
                rec[index]++;
                index++;
            }
            else{
                rec[mp[temp]]++;
            }
        }
        for(i=0;i<index;i++){
            if(rec[i]>max){
                max=rec[i];
                maxi=i;
            }
        }
        auto it=mp.begin();
        while(it!=mp.end()){
            if(it->second==maxi){
                cout<<it->first<<endl;
                break;
            }
            it++;
        }
    }
    return 0;
}

map关联容器的简单应用,把气球颜色与数组下标关联,数组存储个数,最后遍历输出.很简单.