题解 | #成绩排序# Stream流
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static Map map = new LinkedHashMap<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int num = Integer.parseInt(sc.nextLine());
int index = Integer.parseInt(sc.nextLine());
for(int i = 0; i < num; i++){
String[] strs = sc.nextLine().split(" ");
map.put(strs, Integer.parseInt(strs[1]));//用数组当key可以解决name重复的问题
}
map.entrySet().stream().sorted((o1, o2) -> {
if(index == 0){
return o2.getValue() - o1.getValue();
}else{
return o1.getValue() - o2.getValue();
}
}).forEach(o1 -> System.out.println(o1.getKey()[0] + " " + o1.getValue()));
}
}
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static Map
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int num = Integer.parseInt(sc.nextLine());
int index = Integer.parseInt(sc.nextLine());
for(int i = 0; i < num; i++){
String[] strs = sc.nextLine().split(" ");
map.put(strs, Integer.parseInt(strs[1]));//用数组当key可以解决name重复的问题
}
map.entrySet().stream().sorted((o1, o2) -> {
if(index == 0){
return o2.getValue() - o1.getValue();
}else{
return o1.getValue() - o2.getValue();
}
}).forEach(o1 -> System.out.println(o1.getKey()[0] + " " + o1.getValue()));
}
}
成绩排序_牛客题霸_牛客网
https://gw-c.nowcoder.com/api/sparta/jump/link?link=https%3A%2F%2Fwww.nowcoder.com%2Fshare%2Fjump%2F5946710081711106665312
全部评论
相关推荐
03-01 14:27
门头沟学院 计算机类 点赞 评论 收藏
转发
03-04 17:15
重庆大学 电子信息类 点赞 评论 收藏
转发