目录
题目
4664. 字符统计 - AcWing题库https://www.acwing.com/problem/content/4667/
代码
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
//获取数据
Scanner input = new Scanner(System.in);
String str = input.nextLine();
char[] ch = str.toCharArray();
//建立字典
HashMap<Character,Integer> getNum = new HashMap<Character, Integer>();
getNum.put('A',0);
getNum.put('B',1);
getNum.put('C',2);
getNum.put('D',3);
getNum.put('E',4);
getNum.put('F',5);
getNum.put('G',6);
getNum.put('H',7);
getNum.put('I',8);
getNum.put('J',9);
getNum.put('K',10);
getNum.put('L',11);
getNum.put('M',12);
getNum.put('N',13);
getNum.put('O',14);
getNum.put('P',15);
getNum.put('Q',16);
getNum.put('R',17);
getNum.put('S',18);
getNum.put('T',19);
getNum.put('U',20);
getNum.put('V',21);
getNum.put('W',22);
getNum.put('X',23);
getNum.put('Y',24);
getNum.put('Z',25);
HashMap<Integer,String> getLet = new HashMap< Integer,String>();
getLet.put(0,"A");
getLet.put(1,"B");
getLet.put(2,"C");
getLet.put(3,"D");
getLet.put(4,"E");
getLet.put(5,"F");
getLet.put(6,"G");
getLet.put(7,"H");
getLet.put(8,"I");
getLet.put(9,"J");
getLet.put(10,"K");
getLet.put(11,"L");
getLet.put(12,"M");
getLet.put(13,"N");
getLet.put(14,"O");
getLet.put(15,"P");
getLet.put(16,"Q");
getLet.put(17,"R");
getLet.put(18,"S");
getLet.put(19,"T");
getLet.put(20,"U");
getLet.put(21,"V");
getLet.put(22,"W");
getLet.put(23,"X");
getLet.put(24,"Y");
getLet.put(25,"Z");
//计数
int[] appear = new int[26];
int max = 0;
for(int i = 0; i < ch.length; i++){
int index = getNum.get(ch[i]);
appear[index] += 1;
if(appear[index] >= max){
max = appear[index];
}
}
//输出
StringBuilder ans = new StringBuilder();
for(int i = 0; i < appear.length; i++){
if(appear[i] == max){
ans.append(getLet.get(i));
}
}
System.out.println(ans.toString());
}
}
AC结果![](https://img-blog.csdnimg.cn/6490a60922084cd6addc7575c9caf4d3.png)
思路:
这是一道统计题。需要两个字典,一个是通过【字母】找字母的【序号】,一个是通过字母的【序号】找到对应的【字母】。获取输入数据,然后每个字母遍历,某个字母每出现一次就记录下来,并用max变量记录当前出现过最多次数是多少次。当遍历完成时,可知出现过最多次数是多少次,然后再遍历appear,appear的值若为max则以为着当前位置对应的字母是需要输出的,则将其添加到ans中。
一、获取数据
//获取数据
Scanner input = new Scanner(System.in);
String str = input.nextLine();
char[] ch = str.toCharArray();
获取题目输入的数据,并将其转为char数组,方便后续遍历。
二、建立字典
//建立字典
HashMap<Character,Integer> getNum = new HashMap<Character, Integer>();
getNum.put('A',0);
getNum.put('B',1);
getNum.put('C',2);
getNum.put('D',3);
getNum.put('E',4);
getNum.put('F',5);
getNum.put('G',6);
getNum.put('H',7);
getNum.put('I',8);
getNum.put('J',9);
getNum.put('K',10);
getNum.put('L',11);
getNum.put('M',12);
getNum.put('N',13);
getNum.put('O',14);
getNum.put('P',15);
getNum.put('Q',16);
getNum.put('R',17);
getNum.put('S',18);
getNum.put('T',19);
getNum.put('U',20);
getNum.put('V',21);
getNum.put('W',22);
getNum.put('X',23);
getNum.put('Y',24);
getNum.put('Z',25);
HashMap<Integer,String> getLet = new HashMap< Integer,String>();
getLet.put(0,"A");
getLet.put(1,"B");
getLet.put(2,"C");
getLet.put(3,"D");
getLet.put(4,"E");
getLet.put(5,"F");
getLet.put(6,"G");
getLet.put(7,"H");
getLet.put(8,"I");
getLet.put(9,"J");
getLet.put(10,"K");
getLet.put(11,"L");
getLet.put(12,"M");
getLet.put(13,"N");
getLet.put(14,"O");
getLet.put(15,"P");
getLet.put(16,"Q");
getLet.put(17,"R");
getLet.put(18,"S");
getLet.put(19,"T");
getLet.put(20,"U");
getLet.put(21,"V");
getLet.put(22,"W");
getLet.put(23,"X");
getLet.put(24,"Y");
getLet.put(25,"Z");
此处有两个字典,一个(getNum)是通过【字母】找出该字母在26个字母排序中的【序号】,另外一个字典(getLet)则相反,通过其在26个字母排序的【序号】找回对应的【字母】。
三、计数
//计数
int[] appear = new int[26];
int max = 0;
for(int i = 0; i < ch.length; i++){
int index = getNum.get(ch[i]);
appear[index] += 1;
if(appear[index] >= max){
max = appear[index];
}
}
appear数组有26个位置,每一个位置表示对应字母出现过的次数。max表示出现过最多次的次数。读到某个字母,便让该字母出现次数+1。若该字母的出现次数已经超过max,则更新max的值。
四、输出
//输出
StringBuilder ans = new StringBuilder();
for(int i = 0; i < appear.length; i++){
if(appear[i] == max){
ans.append(getLet.get(i));
}
}
System.out.println(ans.toString());
题目要求:输出出现过次数最多的字母,若有多个字母,则按字母表顺序依次输出。只需要根据appear中记录各个字母出现过的次数即可判断某个字母需不需要输出。遍历appear,当appear的值与max相等,则根据getLet字典找到对应的字母加到ans中。最后将ans转为String类型输出。