1042. 字符統計(20)

題目描述

請編寫程序,找出一段給定文字中出現最頻繁的那個英文字母。

輸入格式:

輸入在一行中給出一個長度不超過1000的字符串。字符串由ASCII碼錶中任意可見字符及空格組成,至少包含1個英文字母,以回車結束(回車不算在內)。

輸出格式:

在一行中輸出出現頻率最高的那個英文字母及其出現次數,其間以空格分隔。如果有並列,則輸出按字母序最小的那個字母。統計時不區分大小寫,輸出小寫字母。

輸入樣例:
This is a simple TEST. There ARE numbers and other symbols 1&2&3………..
輸出樣例:
e 7

C++代碼

#include<bits/stdc++.h>
using namespace std;
map<char,int> bet;
int main(){
    string s;
    while(getline(cin,s)){
        int max=0;
        for(int i=0;i<s.length();i++){
            if(isalpha(s[i])){
                bet[tolower(s[i])]++;   
                if(bet[tolower(s[i])]>max)
                    max=bet[tolower(s[i])];
            }   
        }
        for(map<char,int>::iterator i=bet.begin();i!=bet.end();i++)
            if(i->second==max){
                cout<<i->first<<" "<<i->second;
                break;  
            }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章