Problem Description
输入英文句子,输出该句子中除了空格外出现次数最多的字符及其出现的次数。
Input
输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。
Output
逐行输出每个句子中出现次数最多的字符及其出现的次数(如果有多个字符的次数相同,只输出ASCII码最小的字符)。
Example Input
I am a student a good programming problem ABCD abcd ABCD abcd
Example Output
a 2 o 4 A 2
Hint
Authord
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int k,l,m,n,i,j,max;
char s[102];
int t[102];
char st[102];
while(gets(st))
{
m=0;
k=strlen(st);
for(i=0; i<k; i++)
{
if(st[i]!=0&&st[i]!=' ') //对于字符空格不给予讨论
{
n=1;
s[m]=st[i]; //设置一个新的字符串组,将每一个不同字符进入数组
for(j=i+1; j<k; j++)
{
if(st[i]==st[j])
{
st[j]=0; //对有等于前面字符的,进行标识,当循环到它时直接跳过
n++;
}
}
t[m]=n; //每个不同字符出现的次数
m++;
}
}
max=t[0]; //设置最大值为第一个字符的个数
l=0;
//采用打擂法,得到最大值
for(i=1; i<m; i++)
{
if(t[i]>max)
{
max=t[i];
l=i;
}
if(t[i]==max)//当出现字符的个数相等时,通过比较谁的ascill值来获取最大值
{
if(s[i]>s[l])
{
max=t[l];
}
else
{
max=t[i];
l=i;
}
}
}
printf("%c %d\n",s[l],max);
}
return 0;
}
#include <stdlib.h>
#include <string.h>
int main()
{
int k,l,m,n,i,j,max;
char s[102];
int t[102];
char st[102];
while(gets(st))
{
m=0;
k=strlen(st);
for(i=0; i<k; i++)
{
if(st[i]!=0&&st[i]!=' ') //对于字符空格不给予讨论
{
n=1;
s[m]=st[i]; //设置一个新的字符串组,将每一个不同字符进入数组
for(j=i+1; j<k; j++)
{
if(st[i]==st[j])
{
st[j]=0; //对有等于前面字符的,进行标识,当循环到它时直接跳过
n++;
}
}
t[m]=n; //每个不同字符出现的次数
m++;
}
}
max=t[0]; //设置最大值为第一个字符的个数
l=0;
//采用打擂法,得到最大值
for(i=1; i<m; i++)
{
if(t[i]>max)
{
max=t[i];
l=i;
}
if(t[i]==max)//当出现字符的个数相等时,通过比较谁的ascill值来获取最大值
{
if(s[i]>s[l])
{
max=t[l];
}
else
{
max=t[i];
l=i;
}
}
}
printf("%c %d\n",s[l],max);
}
return 0;
}