1004. Ranking of achievements (20)
time limit
400 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CHEN, Yue
Read in the names, student IDs, and grades of n students, and output the names and student IDs of the students with the highest and lowest grades, respectively.
Input format: Each test input contains 1 test case in the format
Line 1: positive integer n
Row 2: The first student's name, student number, grades
Row 3: The name and student number of the second student
... ... ...
Line n+1: The name and student number of the nth student
The name and student ID are both strings of no more than 10 characters, and the grade is an integer between 0 and 100. It is guaranteed that no two students have the same grade in a set of test cases.
Output format: output 2 lines for each test case, the first line is the name and student ID of the student with the highest grade, the second line is the name and student ID of the student with the lowest grade, and there is a space between the strings.
Input sample:3 Joe Math990112 89 Mike CS991301 100 Mary EE990830 95Sample output:
Mike CS991301 Joe Math990112
-----------------------------------------------------------------------------------------------------------
import java.util.Scanner; import java.util.ArrayList;//Introduce list array import java.util.Collections;//Introduce the collection class, use the static method of the collection class to sort the list array public class PAT_B_1004//Submission on the PAT platform needs to change the class name to Main { public static void main(String[] args) { Scanner in = new Scanner(System.in);//Receive all input int n = in.nextInt();//According to the title description, the first one in the input is a number, indicating the number of lines to be entered later in.nextLine();//Skip enter key ArrayList<Student> students = new ArrayList<Student>();//Build an array of student lists for(int i = 0; i < n; i++) { String[] lineSplit = in.nextLine().split(" "); //Call the String.split("String") function to divide the string of each line into 3 strings by spaces. //Pass 3 strings (name, student number, score) to the constructor respectively to construct the student object students.add(new Student(lineSplit[0], lineSplit[1], Integer.parseInt(lineSplit[2]))); //Call Integer.parse("String") to parse the string into a number. } Collections.sort(students);//Call the static sorting function of the collection class, the default ascending order System.out.println(students.get(n-1));//Output the information of the students with the best grades System.out.println(students.get(0));//Output the information of the students with the worst grades } } class Student implements Comparable<Student>//The static sorting function of the collection class can only be called by implementing the Comparable interface {//Construct a student object to save the student's name, student number, and score String name;//Name String stuId;//Student ID int score;//score public Student(String name, String stuId, int score) {//Constructor this.name = name; this.stuId = stuId; this.score = score; } public int compareTo(Student other) {//The compareTo function in the Comparable interface needs to be implemented to call the sorting function in the collection class return (this.score - other.score); } public String toString() {//According to the requirements of the topic, output the name and student number return (this.name + " " + this.stuId); } }