請編寫程序,找出一段給定文字中出現最頻繁的那個英文字母。
輸入格式:
輸入在一行中給出一個長度不超過1000的字符串。字符串由ASCII碼表中任意可見字符及空格組成,至少包含1個英文字母,以回車結束(回車不算在內)。
輸出格式: 在一行中輸出出現頻率最高的那個英文字母及其出現次數,其間以空格分隔。如果有并列,則輸出按字母序最小的那個字母。統計時不區分大小寫,輸出小寫字母。
輸入樣例: This is a simple TEST. There ARE numbers and other symbols 1&2&3………..
輸出樣例: e 7
Answer:
#include<iostream>#define is_upper(c) (c) >= 'A' && (c) <= 'Z'#define is_lower(c) (c) >= 'a' && (c) <= 'z'#define index(c) (c) - 'A' + 'a'using namespace std;int main() { char c; int counts[128] = {}; while((c = cin.get()) != '/n') { if(is_upper(c)) counts[index(c)]++; else if(is_lower(c)) counts[c]++; } int ch = 0, r = 0; for(int i = 'a'; i <= 'z'; i ++) { if(counts[i] > r) { r = counts[i]; ch = i; } } cout << char(ch) << ' ' << r;}PS.
新聞熱點
疑難解答