UVA11577 Letter Frequency【文本】

In this problem we are interested in the frequency of letters in a given line of text. Specifi-cally, we want to know the most
frequently occurring letter(s) in the text, ignoring case (to be clear, “letters” refers precisely to the 26 letters of the alphabet).
在这里插入图片描述
Input
Input begins with the number of test cases on its own line. Each test case consists of a single line of text. The line may contain non-letter characters, but is guaranteed to contain at least one letter and less than 200 characters in total.
Output
For each test case, output a line containing the most frequently occurring letter(s) from the text in lowercase (if there are ties, output all such letters in alphabetical order).
Sample Input
1
Computers account for only 5% of the country’s commercial electricity consumption.
Sample Output
co

问题链接UVA11577 Letter Frequency
问题简述:(略)
问题分析
    统计输入的每一行中各个字母的数量,按字母顺序输出出现频率最高的字母。
    简单的统计题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11577 Letter Frequency */

#include <bits/stdc++.h>

using namespace std;

const int AN = 26;
const int N = 200;
char s[N + 1];
int cnt[AN];

int main()
{
    int n;
    scanf("%d", &n);
    getchar();
    while(n--) {
        gets(s);

        memset(cnt, 0, sizeof(cnt));
        for(int i = 0; s[i]; i++) {
            if(islower(s[i]))
                cnt[s[i] - 'a']++;
            else if(isupper(s[i]))
                cnt[s[i] - 'A']++;
        }

        int maxf = -1;
        for(int i = 0; i < AN; i++)
            maxf = max(maxf, cnt[i]);

        for(int i = 0; i < AN; i++)
            if(cnt[i] == maxf)
                putchar('a' + i);
        putchar('\n');
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值