Problem H: 找出出现次数最多的字母
Description
找出出现次数最多的字母
Input
多组测试,现在给你一行密文,全部由小写字母组成(不超过100个),你要找出出现次数最多的那个字母
Output
每组输出1行,输出出现次数最多的那个字母
Sample Input
aaaaaaabbc
nnnnnnnasnnnnasaaaa
Sample Output
a
n
直接看代码吧
#include <stdio.h>
#include <string.h>
int main()
{
char a[200];
while (gets(a) != NULL)
{
int cnt = 0;
int b[200];
int mmax = 0;
int c[200] = {0};
int len = strlen(a);
for (int i = 0; i < len; i++)
{
b[i] = a[i];
}
for (int i = 0; i < len; i++)
c[b[i]]++;
for (int i = 97; i <= 123; i++)
{
if (c[i] != 0 && c[i] >= mmax)
{
mmax = c[i];
cnt = i;
}
}
printf("%c\n", cnt);
}
}