输入第一行包括一个整数N(1<=N<=100),代表学生的个数。 接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。 如果学生的成绩相同,则按照学号的大小进行从小到大排序。
3 1 90 2 87 3 92
2 87 1 90 3 92
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { int n = scanner.nextInt(); Map<Integer, Integer> myMap = new TreeMap<Integer, Integer>(); for (int i = 0; i < n; i++) { int sno = scanner.nextInt(); int sgrade = scanner.nextInt(); myMap.put(sno, sgrade); } //使用list对entrySet排序 List<Map.Entry<Integer, Integer>> myList = new ArrayList<>(myMap.entrySet()); myList.sort(Map.Entry.comparingByValue()); //创建一个LinkedHashMap,按照排序后的顺序插入entry Map<Integer, Integer> sortedMap = new LinkedHashMap<Integer, Integer>(); for(Map.Entry<Integer, Integer> entry : myList) { sortedMap.put(entry.getKey(), entry.getValue()); } for(Map.Entry<Integer, Integer> entry:sortedMap.entrySet()) { System.out.println(entry.getKey()+" "+entry.getValue()); } } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int n = Integer.parseInt(in.nextLine()); String[] inputs = new String[n]; for(int i=0;i<n;i++){ String[] item = in.nextLine().split(" "); if(Integer.parseInt(item[0])<10){ //因为n<=100,补零为了后面排序 item[0] = "0"+item[0]; } inputs[i] = item[1]+" "+item[0]; } //排序 Arrays.sort(inputs); for(int i=0;i<n;i++){ String[] item = inputs[i].split(" "); if(Integer.parseInt(item[1])<10){ //去掉前面补的零 item[1] = item[1].substring(1,2); } String temp = item[1]+" "+item[0]; System.out.println(temp); } } } }
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); ArrayList<Student> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(new Student(scanner.nextInt(),scanner.nextInt())); Collections.sort(list); for (Student student : list) System.out.println(student); } public static class Student implements Comparable<Student>{ Integer id; Integer score; public Student(Integer id,Integer score){ this.id=id; this.score=score; } @Override public int compareTo(Student o) { return this.score.equals(o.score) ?id.compareTo(o.id):score.compareTo(o.score); } @Override public String toString() { return id+" "+score; } } }解法二:
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine(); ArrayList<String> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(scanner.nextLine()); list.sort((o1, o2) -> { String[] s1 = o1.split(" "); Integer id1 = Integer.parseInt(s1[0]); Integer score1= Integer.parseInt(s1[1]); String[] s2 = o2.split(" "); Integer score2= Integer.parseInt(s2[1]); Integer id2 = Integer.parseInt(s2[0]); return score1.equals(score2) ?id1.compareTo(id2):score1.compareTo(score2); }); for (String s : list) System.out.println(s); } }
public class Main { public static void main(String[]args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Student[] students = new Student[n]; for(int i=0;i<n;i++){ int number = scanner.nextInt(); int grade = scanner.nextInt(); students[i] = new Student(grade, number); } Arrays.stream(students) .sorted((o1, o2) -> o1.grade == o2.grade ? o1.number - o2.number : o1.grade - o2.grade) .forEach(System.out::println); } } class Student{ int grade; int number; public Student(int grade, int number) { this.grade = grade; this.number = number; } @Override public String toString() { return number + " " + grade; } }
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Student {
private int id;
private int grade;
public Student(int id, int grade) {
this.id = id;
this.grade = grade;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setGrade(int grade) {
this.grade = grade;
}
public int getGrade() {
return grade;
}
public String toString() {
return id + " " + grade;
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
Student[] arr = new Student[n];
for (int i = 0; i < arr.length; i++) {
int id = scan.nextInt();
int grade = scan.nextInt();
arr[i] = new Student(id, grade);
}
Arrays.sort(arr, new MyComparator());
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i].toString());
}
}
}
private static class MyComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
if (s1.getGrade() != s2.getGrade()) {
return s1.getGrade() > s2.getGrade() ? 1 : -1;
} else {
return s1.getId() > s2.getId() ? 1 : -1;
}
}
}
}
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; /** * Created by fhqplzj on 17-1-26 at 下午1:08. */ public class My4 { static class Student implements Comparable<Student> { private int index; private int score; Student(int index, int score) { this.index = index; this.score = score; } @Override public String toString() { return String.format("%d %d", index, score); } @Override public int compareTo(Student o) { if (score != o.score) { return score < o.score ? -1 : 1; } else { return new Integer(index).compareTo(o.index); } } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Student> students = new ArrayList<>(); while (scanner.hasNextInt()) { int n = scanner.nextInt(); for (int i = 0; i < n; i++) { students.add(new Student(scanner.nextInt(), scanner.nextInt())); } Collections.sort(students); for (Student student : students) { System.out.println(student); } } } }