题目描述
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/16a29d63e364bf1866a5c4ccc48e326b.png)
算法结果
3 3
math chinese english
z3 71 81 91
l4 81 71 93
w5 21 91 95
math
l4 81 71 93 245
z3 71 81 91 243
w5 21 91 95 207
算法详情
public class SmartScoreTable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] inputNumber = scanner.nextLine().split(" ");
int n = Integer.parseInt(inputNumber[0]);
int m = Integer.parseInt(inputNumber[1]);
String[] subjects = scanner.nextLine().split(" ");
String[][] scores = new String[n][m + 2];
for (int i = 0; i < n; i++) {
String[] score = scanner.nextLine().split(" ");
System.arraycopy(score, 0, scores[i], 0, score.length);
}
String sortSubject = scanner.nextLine();
scanner.close();
calTotalScore(scores);
int index = Arrays.stream(subjects).collect(Collectors.toList()).indexOf(sortSubject);
List<String[]> result = Arrays.stream(scores).collect(Collectors.toList());
result.sort(new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
return compareRule(o1, o2, index);
}
});
for (String[] score : result) {
String output = "";
for (String str : score) {
output = output + str + " ";
}
System.out.println(output);
}
}
private static int compareRule(String[] str1, String[] str2, int index) {
int len = str1.length;
if (index == -1) {
if (Integer.parseInt(str2[len - 1]) - Integer.parseInt(str1[len - 1]) == 0) {
return str1[0].compareTo(str2[0]);
}
return Integer.parseInt(str2[len - 1]) - Integer.parseInt(str1[len - 1]);
} else {
if (Integer.parseInt(str2[index + 1]) - Integer.parseInt(str1[index + 1]) == 0) {
return str1[0].compareTo(str2[0]);
}
return Integer.parseInt(str2[index + 1]) - Integer.parseInt(str1[index + 1]);
}
}
private static void calTotalScore(String[][] scores) {
int rows = scores.length;
int cols = scores[0].length;
for (int i = 0; i < rows; i++) {
int total = 0;
for (int j = 1; j < cols - 1; j++) {
total += Integer.parseInt(scores[i][j]);
}
scores[i][cols - 1] = Integer.toString(total);
}
}
}