我是靠谱客的博主 小巧外套,这篇文章主要介绍Bailian2742 Number of letters【入门】,现在分享给大家,希望可以做个参考。

2742:Number of letters
描述
Find out the letter that occurs the most times in a string that consistes
of ‘a’-‘z’.

输入
Line 1: an integer N, indicates the number of test case

Line 2, 4, 6…2N: a string consistes of ‘a’-‘z’, of which the size is from 1 to 1000 inclusive

LIne 3, 5, 7…2N-1: blank line
输出
Output N lines corresponding N strings of inputs.

Each line contains a letter that occurs the most times in the corresponding string and a number that is the time of occurence, seperated by a blank space.
样例输入
2
abbccc

adfadffasdf
样例输出
c 3
f 4

问题链接:Bailian2742 Number of letters
问题描述:(略)
问题分析
    这个问题是小写字母统计问题,用个数组统计一下就好了。关键是要注意输入数据(字符串)是隔行的,所以程序使用格式化输入函数scanf()读入字符串,并且指定格式为"%s",以便跳过空行。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C语言程序如下:

/* Bailian2742 Number of letters */

#include <stdio.h>
#include <string.h>

#define N 26
#define M 1000

char s[M + 1];

int main(void)
{
    int n;

    scanf("%d", &n);
    while(n--) {
        int cnt[N], i;

        /* 统计字符数 */
        scanf("%s", s);
        memset(cnt, 0, sizeof(cnt));
        for(i = 0; s[i]; i++)
            cnt[s[i] - 'a']++;

        /* 找出出现次数最多的字符 */
        int max = 0;
        for(i = 0; i < N; i++)
            if(cnt[i] > cnt[max]) max = i;

        printf("%c %dn", 'a' + max, cnt[max]);
    }

    return 0;
}

最后

以上就是小巧外套最近收集整理的关于Bailian2742 Number of letters【入门】的全部内容,更多相关Bailian2742内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(128)

评论列表共有 0 条评论

立即
投稿
返回
顶部