文章

21

粉丝

0

获赞

6

访问

1.5k

头像
字母频率 题解:
P1019 贵州大学机试题
发布于2026年1月27日 15:13
阅读数 24

Map+cctype判断字母与大小写转换函数写法

 

#include <iostream>
#include <map>
#include <cctype>   // for isalpha, tolower
#include <string>
using namespace std;

int main() {
    string s;
    getline(cin, s);  // 读取整行输入

    map<char, int> freq;  // C++98 中可用 map 模拟哈希表(有序,但不影响结果)

    // 遍历字符串中的每个字符(C++98 风格)
    for (string::size_type i = 0; i < s.length(); ++i) {
        char c = s[i];
        if (isalpha((unsigned char)c)) {  // 安全起见,转为 unsigned char
            char lower_c = tolower((unsigned char)c);
            freq[lower_c]++;
        }
    }

    // 如果没有英文字母,题目未说明,但通常假设有
    char most_char = 'a';
    int max_count = 0;

    // 遍历 'a' 到 'z' 找最大频次(保证字典序最小优先)
&nbs...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发