标签:
用一维数组存储学号和成绩,然后,按成绩排序输出。
输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
如果学生的成绩相同,则按照学号的大小进行从小到大排序。
3
1 90
2 87
3 92
2 87
1 90
3 92
import java.util.Arrays;import java.util.Comparator;import java.util.Scanner; public class Main { private static class Student{ private int stuno; private int grade; public Student(int stuno,int grade){ this.stuno = stuno; this.grade = grade; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()){ int n = in.nextInt(); Student[] stus = new Student[n]; for (int i = 0; i < n; i++) { stus[i] = new Student(in.nextInt(), in.nextInt()); } Arrays.sort(stus, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { if(o1.grade!=o2.grade) return o1.grade-o2.grade; return o1.stuno-o2.stuno; } }); for (Student student : stus) { System.out.println(student.stuno+" "+student.grade); } } in.close(); } }标签:
原文地址:http://www.cnblogs.com/bb3q/p/5096801.html