题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String read;
while ((read = in.readLine()) != null) {
int num = Integer.parseInt(read);
for(int i = 0; i < num; i++) {
char[] ch = in.readLine().toCharArray();
int[] cal = new int[26];
for(int j = 0; j < ch.length; ++j) {
cal[ch[j] - 'a']++;
}
int max = 0;
int rec = 0;
//冒泡排序,每次外层for循环获取当前最大值
for(int j = cal.length; j >=0 ; j--) {
int pos = 0;
for(int k = 1; k < j; ++k) {
if(cal[k - 1] > cal[k]) {
int tem = cal[k - 1];
cal[k - 1] = cal[k];
cal[k] = tem;
}
pos = k;
}
max += cal[pos] * (26 - rec);
rec += 1;
}
System.out.println(max);
}
}
}
}
查看10道真题和解析