题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
int n = Integer.parseInt(in.nextLine());
String[] strs = new String[n];
for(int i=0;i<n;i++) {
strs[i]=in.nextLine();
}
for(String str : strs) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for(Character C : str.toCharArray()) { //统计每个字母出现的次数
Character c = Character.toLowerCase(C);
if(!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c)+1);
}
}
Integer[] sums = new Integer[map.size()];
int count=0;
for(Character c : map.keySet()) {
sums[count]=map.get(c);
count++;
}
Arrays.sort(sums, Collections.reverseOrder()); //将map的值提取出来并逆序排序
int sum = 0;
int beautiful = 26;
for(int i : sums) { //出现最多的给最大的,累加计算
sum+=i*beautiful;
beautiful--;
}
System.out.println(sum);
}
}
}
}
无脑暴力
科大讯飞公司氛围 425人发布