题目描述
小蓝正在学习一门神奇的语言,这门语言中的单词都是由小写英文字母组 成,有些单词很长,远远超过正常英文单词的长度。小蓝学了很长时间也记不住一些单词,他准备不再完全记忆这些单词,而是根据单词中哪个字母出现得最多来分辨单词。
现在,请你帮助小蓝,给了一个单词后,帮助他找到出现最多的字母和这 个字母出现的次数。
输入描述
输入一行包含一个单词,单词只由小写英文字母组成。
对于所有的评测用例,输入的单词长度不超过 1000。
输出描述
输出两行,第一行包含一个英文字母,表示单词中出现得最多的字母是哪 个。如果有多个字母出现的次数相等,输出字典序最小的那个。
第二行包含一个整数,表示出现得最多的那个字母在单词中出现的次数。
样例
示例 1
输入
lanqiao
输出
a
2
示例 2
输入
longlonglongistoolong
输出
o
6
python代码:
import os
import sys
word = input()
a = 0
b = []
for i in word:
c = word.count(i)
if c >= a:
a = c
for j in word:
if word.count(j) == a:
b.append(j)
b.sort()
print(b[0])
print(a)
C语言代码:
#include<stdio.h>
#include<string.h>
int main(){
char str[1010];
scanf("%s",str);
int max = 0;
char a[1];
a[0] = str[0];
for(int i = 0 ;i < strlen(str) ;i++){
int num = 1;
for(int j = i + 1; j < strlen(str) ; j++){
if(str[i] == str[j]){
num++;
}
}
if(num > max){
a[0] = str[i];
max = num;
}else if(num == max){
max = num;
if(str[i] < a[0]){
a[0] = str[i];
}
}
}
printf("%c\n",a[0]);
printf("%d\n",max);
return 0;
}
知识点:
python中count函数的用法:
python 的sort()函数详解_qq_20831401的博客-CSDN博客_python sort
本文含有隐藏内容,请 开通VIP 后查看