资讯动态

01:谁考了第k名

发布时间:2026/11/6 1:19:42 来源:尧图企业网站定制
/** * 题目名称谁考了第k名 p * 题目来源http://noi.openjudge.cn/ch0110/01/ p * 程序功能输出指定名次学生的学号和成绩 * * author 潘磊just_panleijust.edu.cn * version 1.0 */importjava.util.List;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Scanner;classStudentimplementsComparableStudent{// 学生类实现Comparable接口intid;// 学号doublescore;// 成绩publicStudent(intid,doublescore){// 构造方法初始化学生记录this.idid;this.scorescore;}Override// 重写compareTo方法按score降序排序publicintcompareTo(Studento){if(o.scorethis.score){// 如果两条记录的score相等return0;// 保持位置不变}returno.scorethis.score?1:-1;// 按score降序排序}Override// 重写toString方法按题目输出要求返回字符串publicStringtoString(){returnString.format(%d %.1f,id,score);}}publicclassMain{publicstaticvoidmain(String[]args){ScannerinputnewScanner(System.in);intninput.nextInt();intkinput.nextInt();intid;doublescore;ListStudentstudentsnewArrayList();for(inti0;in;i){idinput.nextInt();scoreinput.nextDouble();students.add(newStudent(id,score));}Collections.sort(students);// 按自定义规则对students列表排序System.out.print(students.get(k-1).toString());}}

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑