hdu1004 统计出现最多的颜色数(map容器☆☆☆☆☆)

很简单的一道题,利用map容器本应该直接就过的,居然WA了好多次。

后来才发现,丢了一条更新统计结果的语句,以此为戒,以后严加注意!!!

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <string>

using namespace std;

int main(){

    int n;
    map<string,int> countMap;
    string word, tmp;
    char color[32];
    int maxCount;

    while(scanf("%d",&n)&&n){
        maxCount = 0;
        for(int i=0;i<n;i++){
            scanf("%s",color);
            tmp = color;
            if(countMap.find(tmp)==countMap.end()){
                countMap[tmp]=1;
            }
            else{
                countMap[tmp]++;
            }

            if(countMap[tmp]>maxCount){
                word = tmp;
                maxCount = countMap[tmp];
            }
        }

        printf("%s
",word.c_str());
        countMap.clear();

    }

    return 0;
}

  

原文地址:https://www.cnblogs.com/baipengchao/p/3869174.html