题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b?tpId=37&tqId=21291&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=68
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
int num = in.nextInt();
int Sort = in.nextInt();
String[][] str = new String[num + 1][2];
// 注意 hasNext 和 hasNextLine 的区别
ArrayList<Integer> Test = new ArrayList<>();
for (int i = 0; i < num; i++) {
//不知道为什么nextLine后用split()无法装入数组中只能选择手动分别装入
// String r=in.nextLine();
// str[i] = r.split(" ");
// String[] p = r.split(" ");
// System.out.println(in.nextLine());
// System.out.println(p[0]+" "+p[1]);
str[i][0]=in.next();
str[i][1]=in.next();
int core = Integer.parseInt(str[i][1]);
if (!Test.contains(core)) {
Test.add(core);
}
if (!map.containsKey(core)) {
ArrayList<Integer> list = new ArrayList<>();
list.add(i);
map.put(core, list);
}
else
{
ArrayList<Integer> Update = map.get(core);
Update.add(i);
}
}
Collections.sort(Test);
if (Sort != 0)
{
for (int i = 0; i < Test.size(); i++) {
ArrayList<Integer> li = map.get(Test.get(i));
for (int j : li) {
System.out.println(str[j][0] + " " + str[j][1]);
}
}
}
else {
for (int i = Test.size()-1; i>=0; i--) {
ArrayList<Integer> li = map.get(Test.get(i));
for (int j : li) {
System.out.println(str[j][0] + " " + str[j][1]);
}
}
}
}
}
