S - Number of letters
Find out the letter that occurs the most times in a string that consistes
of 'a'-'z'.Input
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 lineOutput
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.Sample Input
复制代码1
2
3
4
52 abbccc adfadffasdfSample Output
复制代码1
2
3c 3 f 4
题意:
找到对应字符串中出现次数最多的小写字母
题解:
01 map[26]={"abcdefghijklmnopqrstuvwxyz"}; 进行映射
02 (不用映射也行)用下标做一个加法
03 小心输入字符串时中间的空行
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53// S - Number of letters #include<stdio.h> #include<string.h> int main() { int n,i,count,pos; int map[26]; char str[1005],ch; // 老规矩 %*c 吃回车 scanf("%d%*c",&n); count=0; while( count<n ) { gets( str ); // 跳过空行 if( str[0]=='' ) continue; count++; memset( map,0,sizeof( map ) ); // 计数 for( i=0;str[i];i++ ) { map[ str[i]-'a' ]++; } pos=0; ch='a'; // 找出现次数最多的小写字母 for( i=1;i<26;i++ ) { if( map[pos]<map[i] ) { // 用下标做加法 也能找到对应小写字母 pos=i; ch='a'+i; } } printf("%c %dn",ch,map[pos]); } return 0; }
个人见解 酌情采纳.
最后
以上就是鲤鱼犀牛最近收集整理的关于【UJN-寒假练练练】新年快乐-2-进制转换与字符串_S - Number of letters的全部内容,更多相关【UJN-寒假练练练】新年快乐-2-进制转换与字符串_S内容请搜索靠谱客的其他文章。
发表评论 取消回复