字符统计2

字符统计2

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

输入英文句子,输出该句子中除了空格外出现次数最多的字符及其出现的次数。

Input

输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。

Output

逐行输出每个句子中出现次数最多的字符及其出现的次数(如果有多个字符的次数相同,只输出ASCII码最小的字符)。

Sample Input

I am a student
a good programming problem
ABCD abcd ABCD abcd

Sample Output

a 2
o 4
A 2

Hint

 这个题的精髓就是用ASCII码来表示下标,这样每次出现相同的字母,就自动加到了b下标对应的ASCII中,最后的for(i = 0; i < 200; i++)  这里0和200不是固定的,只要大于'z'的ASCII码小于'A'的ASCII码即可
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char a[400];
 6     int i, j, t,len, max, max1;
 7     int b[400];
 8     while (gets(a))
 9     {
10         memset(b, 0, sizeof(b));
11         len = strlen(a);
12         for (i = 0; i < len; i++)
13         {
14             if (a[i] != ' ')
15             {
16                 b[a[i]]++;//用ASCII码做下标
17             }
18         }
19         max = 0;//max是最大次数
20         max1 = 0;//max1是i所对应的字母
21         for (i = 0; i < 200; i++)
22         {
23             if (max < b[i]){
24                 max = b[i];
25                 max1 = i;
26             }
27         }
28         printf("%c %d
", max1, max);
29     }
30     return 0;
31 }

Source

原文地址:https://www.cnblogs.com/52dxer/p/9935950.html