1042 字符统计

水题,又是一道hash题。

#include<iostream>
#include<cctype>
using namespace std;

int hashtable[26]= {0};
int main() {
    char c;
    while(scanf("%c",&c)!=EOF) {
        if(isalpha(c)) {//是字母?
            if(isupper(c))//大写字母转小写字母
                c+=32;
            hashtable[c-'a']++;
        }
    }
    int max = 0,pos = -1;
    for(int i = 0; i < 26; ++i)
        if(max < hashtable[i]) {
            max  = hashtable[i];
            pos = i;
        }
    printf("%c %d",pos+'a',max);
    return 0;
}

 

posted @ 2020-02-19 18:03  tangq123  阅读(190)  评论(0编辑  收藏  举报