1.字符统计
1.1问题叙述
给定一个只包含大写字母的字符串S,请你输出其中出现次数最多的字母。如果有多个字母均出现最多次,按字母表顺序依次输出所有这些字母
1.2思路
创建一个26个元素的数组,分别对应字母表。统计字符串中字母出现的次数,赋值到字母表数组中。统计数组中最大值并输出
1.3代码
public class ZFFX {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符");
String S = sc.next();
int length = S.length();
int[] array = new int[26];
int i,max = 0;
for(i = 0; i < length; i++) {
array[S.charAt(i) - 'A']++;
max = Math.max(max, array[S.charAt(i) - 'A']);
}
for(i = 0; i < 26; i++ ) {
if(max == array[i])
System.out.printf("%c",(char)(i + 'A'));
}
}
}