java 实例 按CGPA大小排列名字
2017-03-17 20:52 backyyan 阅读(475) 评论(0) 编辑 收藏 举报要求:
2<=N<=1000
0<=ID<=100000
5<=|NAME|<=30
0<=CGPA<=4.0
N表示个数
输入:
5 33 Rumpa 3.68 85 Ashis 3.85 56 Samiha 3.75 19 Samara 3.75 22 Fahim 3.76
输出:
Ashis
Fahim
Samara
Samiha
Rumpa
代码:
import java.util.*; class Student { public int id; public String fname; public double cgpa; public Student(int id, String fname, double cgpa) { this.id = id; this.fname = fname; this.cgpa = cgpa; } } class Checker implements Comparator<Student> { @Override public int compare(Student p, Student q) { if (q.cgpa == p.cgpa) { if(p.fname == q.fname) { return q.id - p.id; } return p.fname.compareTo(q.fname); } else { return q.cgpa > p.cgpa ? 1 : -1; } } } public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = Integer.parseInt(scan.nextLine()); Student[] student = new Student[n]; Checker checker = new Checker(); for(int i = 0; i < n; i++) { int id = scan.nextInt(); String fname = scan.next(); double cgpa = scan.nextDouble(); student[i] = new Student(id, fname, cgpa); } Arrays.sort(student, checker); for(Student st : student) { System.out.println(st.fname); } } }