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语言程序如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35/* 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内容请搜索靠谱客的其他文章。
发表评论 取消回复