找出出现次数最多的字母

找出出现次数最多的字母
Description
找出出现次数最多的字母Input现在给你一行密文,全部由小写字母组成(不超过100个),你要找出出现次数最多的那个字母
Output
每组输出1行,输出出现次数最多的那个字母
Sample Input
aaaaaaabbc
nnnnnnnasnnnnasaaaa
Sample Output
a
n

#include<stdio.h>
#include<string.h>
int main()
{
 	int i,j,len,t;
 	char str[110];
 	char ch[27]={"abcdefghijklmnopqrstuvwxyz"};
 	while(gets(str)!=NULL)
 	{
  		int m;
  		int num[30];
  		memset(num,0,sizeof(num));
  		len=strlen(str);
  		int max=0;
  		for(i=0;i<len;i++)
  		{
   			t=str[i]-'a';
   			num[t]++; 
   			if(num[t]>max)
   			{
    				max=num[t];
    				m=t;
   			} 
  		}
  		printf("%c\n",ch[m]);
 	}
} 

scanf不可以读入空格和换行符;gets可以读入空格,也可以读入前一个输入的换行符,这里用或者不用getchar()(emmm有点诡异,but oj 是这样的);
然后如果输入变成scanf("%s",str)!=EOF用或不用getchar()也可以。
用ASCII码

猜你喜欢

转载自blog.csdn.net/weixin_43613299/article/details/85645433