[JAVA]1083 List Grades (25分)

Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval.

Input Specification:

Each input file contains one test case. Each case is given in the following format:

N
name[1] ID[1] grade[1]
name[2] ID[2] grade[2]
... ...
name[N] ID[N] grade[N]
grade1 grade2

where name[i] and ID[i] are strings of no more than 10 characters with no space, grade[i] is an integer in [0, 100], grade1 and grade2 are the boundaries of the grade’s interval. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case you should output the student records of which the grades are in the given interval [grade1, grade2] and are in non-increasing order. Each student record occupies a line with the student’s name and ID, separated by one space. If there is no student’s grade in that interval, output NONE instead.

很简单的对分数范围的选择…
用了treeSet。。 似乎有点大材小用?

import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * @author Byrce
 * @create 2020-01-13 20:26
 */
public class Main {
    public static void main(String[] args) {
        int sum;
        Scanner scan = new Scanner(System.in);
        sum = scan.nextInt();
        TreeSet<Student> s = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getGrade()-o1.getGrade();
            }
        });
        for (int i = 0; i < sum; i++) {
            Student stu = new Student();
            stu.setName(scan.next());
            stu.setID(scan.next());
            stu.setGrade(scan.nextInt());
            s.add(stu);
        }
        //学生添加完成
        int minGrade = scan.nextInt();
        int maxGrade = scan.nextInt();
        int cnt = 0;
        for(Student ss : s){
            if(ss.getGrade() <= maxGrade && ss.getGrade()>=minGrade){
                System.out.println(ss);
                cnt++;
            }
        }
        if(cnt == 0){
            System.out.println("NONE");
        }
    }
}
class Student {
    private String name;
    private String ID;
    private int grade;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return name+" "+ID;
    }
}
发布了11 篇原创文章 · 获赞 0 · 访问量 172

猜你喜欢

转载自blog.csdn.net/Bryce_Loski/article/details/103964501