题干 C++实现 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<map> using namespace std; int main() { //如果输入带空格的字符串(scanf不行)?eg:This is An Pencil Case //1.fgets() //2.cin.getline(); char arr[200] = { 0 }; fgets(arr, 200, stdin); int i = 0; int alphaCount = 0; bool isSpace = true;//标志前一个字符是否是空格 int wordCount = 0; map<char, int> alphaMap; while (true) { if (arr[i] == '\0' || arr[i] == '\n') { break; } else if (arr[i] == ' ') { isSpace = true; } else { ++alphaCount; if (isSpace) { wordCount++; } if (arr[i] >= 'A' && arr[i] <= 'Z') { arr[i] += 32;//大写字母变小写字母 } ++alphaMap[arr[i]]; isSpace = false; } i++; } printf("%d\n", alphaCount); printf("%d\n", wordCount); map<char, int>::iterator it; int maxTimes = 0; for (it = alphaMap.begin(); it != alphaMap.end(); it++) { if (it->second > maxTimes) { maxTimes = it->second; } } for (it = alphaMap.begin(); it != alphaMap.end(); it++) { if (it->second == maxTimes) { printf("%c ", it->first); } } printf("\n"); printf("%d\n",maxTimes); return 0; }