题目:
代码:
描述 | |
---|---|
知识点 | 字符串 |
运行时间限制 | 0M |
内存限制 | 0 |
输入 | 整数N,后续N个名字 N个字符串,每个表示一个名字 |
输出 | 每个名称可能的最大漂亮程度 |
样例输入 | 2 zhangsan lisi |
样例输出 | 192 101 |
import java.util.Scanner;
public class Main {
public static int[] sort(int[] arr) {
int temp = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] > arr[i]) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
return arr;
}
public static int MaxBeauty(String str) {
int[] Num = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
for (int j = i + 1; j < str.length() && Num[i] >= 0; j++) {
if (Num[i] == 0) {// 第一次进该循环,为其赋值
Num[i] = 1;
}
if (c == str.charAt(j)) {
Num[i]++;
Num[j] = -1;
}
}
if (Num[i] == 0) {// 最后的字母如果不是-1,也无法进循环得出的值会为0
Num[i] = 1;
}
}
int[] sortedNum = sort(Num);
int Beauty = 0;
for (int i = 0; i < sortedNum.length; i++) {
if (sortedNum[i] > 0) {
Beauty = sortedNum[i] * (26 - i) + Beauty;
}
}
return Beauty;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int name_Num = input.nextInt();
String a = input.nextLine();// 将换行符读取,否则会读入字符串数组
String[] name = new String[name_Num];
for (int i = 0; i < name_Num; i++) {
name[i] = input.nextLine();
}
for (int i = 0; i < name_Num; i++) {
System.out.println(MaxBeauty(name[i]));
}
}
}
可用选择排序,冒泡排序等。