题解 | #名字的漂亮度#
名字的漂亮度
http://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
package com.hikvision.niuke;
import java.util.Arrays;
import java.util.Scanner;
/**
* @Classname BeautifulDegree
* @Description 漂亮值
* @Date 2022/2/13 18:23
* @Created by daihuhu
*/
public class BeautifulDegree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int num = scanner.nextInt();
for (int i = 0; i < num; i++) {
String name = scanner.next().toLowerCase();
getBeautifulDegree(name);
}
}
}
private static void getBeautifulDegree(String name) {
int[] array = new int[26];
char[] chars = name.toCharArray();
//统计每个字母出现的次数
for (char ch : chars) {
array[ch - 97] += 1;
}
int beautifulDegree = 0;
//为了使漂亮值达到最大,对数组进行排序,次数出现越多的字符,使其漂亮值越大,进而整个名字的漂亮值越大
Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
beautifulDegree += array[i] * (i + 1);
}
System.out.println(beautifulDegree);
}
}