每日作业贰
成绩排名
输入多个学生的姓名和成绩, 打印其名称 成绩和排名
输入:
第一行: 整数N 表示有N个学生
第二行开始, 每行一个字符串和一个整数Mi, 表示学生姓名和成绩
输出:
(1<=N<=100, 0<=Mi<=100)
按成绩从高到低的顺序打印每个学生的姓名,成绩, 排名
(需要注意的是, 如果成绩相同, 则排名并列)
样例输入
6
张三 80
李四 90
王五 85
赵六 85
李雷 90
韩梅梅 100
样例输出:
韩梅梅 100 1
李雷 90 2
李四 90 2
王五 85 3
赵六 85 3
张三 80 4
1 package com.work; 2 3 import java.util.*; 4 import java.util.stream.Collectors; 5 6 /** 7 * @author My 8 */ 9 public class Assignment_13 { 10 11 public static void main(String[] args) { 12 Map<String,Integer> map=new HashMap<>(); //映射 13 Scanner reading = new Scanner(System.in); 14 System.out.print("输入学生数:"); 15 int N = reading.nextInt(); 16 String name; 17 int score; 18 for (int i = 0; i < N; i++) { 19 System.out.println("输入姓名:"); 20 name= reading.next(); 21 System.out.println("输入成绩:"); 22 score = reading.nextInt(); 23 map.put(name,score); 24 } 25 26 List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet()); //哈希排序 27 Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { 28 @Override 29 public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { 30 // return o2.getValue().compareTo(o1.getValue()); 31 return o2.getValue()- o1.getValue(); 32 } 33 }); 34 int n =1; 35 System.out.println(list.get(0).getKey()+" "+list.get(0).getValue()+" "+n); 36 for (int i = 1; i < list.size(); i++) { //输出 37 if (list.get(i).getValue().equals(list.get(i - 1).getValue())){ 38 System.out.println(list.get(i).getKey()+" "+list.get(i).getValue()+" "+n); 39 }else { 40 n++; 41 System.out.println(list.get(i).getKey()+" "+list.get(i).getValue()+" "+n); 42 } 43 } 44 } 45 }

浙公网安备 33010602011771号