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
Source
本题要注意 大小写以及大小写字母的asll码值之间有六个非字母
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100];
int i,n,k,max=0;
while(gets(a)!=NULL) //多组输入,要以NULL结束,不能以EOF结束;
{
int b[58]= {0}; //‘z’和‘A’之间的asll表中不是52个字符;
max=0;
n=strlen(a);
for(i=0; i<n; i++)
{
if((a[i]<='z'&&a[i]>='a')||(a[i]<='Z'&&a[i]>='A'))
//是字母便执行下面语句,避免非字母干扰;
{
int k = a[i]-'A';
b[k]++;
}
}
for(i=0; i<58; i++)
{
if(b[i]>max)
{
max=b[i];
k=i;
}
}
printf("%c %d\n",'A'+k,max); /*上面对每一个字符减“A”,
所以在这里加“A”便是原来的字符;*/
}
return 0;
}
运行结果:
I am a student
a 2
a good programming problem
o 4
ABCD abcd ABCD abcd
A 2
^Z
Process returned 0 (0x0) execution time : 8.526 s
Press any key to continue.