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\n",word.c_str());
        countMap.clear();
    }
    return 0;
}
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号