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 %d\n", 'a' + max, cnt[max]);
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tigerisland45/p/9986717.html