Algorithmic Improvement of Primary Scholarship java

Algorithmic Improvement of Primary Scholarships
Problem Description~
There are n students in this university.
Each of them gets a percentage integer score (0-100) in each of the m required courses.
The top k students are selected and awarded scholarships.
A student who fails one course (with a score of less than 85) loses his qualification for a scholarship.
The Dean ranks all subjects in order of importance. Among all students who are eligible for scholarships, the Dean prefers students who have high scores in more important subjects.
The students were ranked accordingly (that is, for one or two students, those who scored higher in the most important subjects were ranked first;
If two people get the same score in the most important subjects, then the less important ones are compared; if they are the same, the third important ones are compared...Keep going).
When Tuhao University used to have a small number of students, the Dean used pen and paper to calculate who would win the scholarship. When Tuhao expanded its enrollment this year, the Dean realized that pen and paper calculation was too complicated.
Can you help him solve this problem?Maybe the dean will give you a scholarship if he is happy.(Note: The number of award-winning students may be less than k)

Input Format~
The first behavior of input data is three integers n, m, k separated by spaces.
Lines 2 to 1 (n+1) are numbers 1, 2...N's performance in M classes (m classes are sorted by importance),
Separate them with spaces (input guarantees that each person's score is not exactly the same).
Output Format~
Output the number of a behavior winner, sorted from top to bottom, separated by spaces.
sample input
10 2 7
99 87
96 79
93 99
88 88
90 87
90 86
90 88
88 90
91 100
100 85
Sample Output~
10 1 3 9 7 5 6
Data size and conventions~

	 Student: n (attribute: num and grade[])
    Required Course: m-gate 
Maximum number of winners: k
 Filtering method:
		a: Compare the results from the first required course, and if the results are the same, compare the next one, that is (the higher the score, the higher the priority of the required course, the higher the priority.)And take precedence over the first k names.			
		b: If one of your failures (<85), you will be disqualified from the scholarship.
Note: The number of eligible scholarships may be less than k.

To compare students, you need to customize a student's class.(results should be stored in an int array)
Now that you want to compare objects by student performance, you think you need to inherit Comparable and override the compareTo method.Therefore, how to override the compareTo method is the key to the problem.
After that, the data is initialized.Each object is stored in a list and simulated filters are performed using collections.sort(list).
Number of min(k, list.length) students before output

import java.util.*;

public  class Main {
	//m is a static variable
    static  int m; 
    //Define internal class inherits Comparable
    public static class student implements Comparable{
        int num ;
        int[] grade ;
        public student(int num,int[] grade){
        //Override compareTo method, call compare method
        public int compareTo(Object o) {
            student student = (student) o;
            return compare(this.grade, student.grade);
        public  int compare(int[] x, int[] y){
       //Returns 1/-1 for inequality in score i I and goes to the next cycle for equality
            for (int i=1;i<=m;i++){
                if (x[i]-y[i]>0){
                    return -1;
                }else if (x[i]-y[i]<0){
                    return 1;
            //For error-free nonsense
            return  0;
		//Override toString method
        public String toString() {
                return  num +" ";
    public static void main(String[] args) {
        Scanner scanner =new Scanner(;
        //Initialize n, m, k
        int n =scanner.nextInt();		//Number of people
        m=scanner.nextInt();				//Number of disciplines
        int k=scanner.nextInt();			//Maximum number of winners
        List<student> list =new ArrayList();
        int num=0;            //Student number 
        //Initialize Student Properties
  		b: for (int i=0;i<n;i++){
            int[] grade = new int[m+1];
            num++;    //Ensure that the student number is correct
           for (int j=1;j<=m;j++){
                grade[j]=scanner.nextInt(); //Guarantee that the output is finished before you decide if you are qualified
           for (int j=1;j<=m;j++){
           //Failures are not saved in the list
               if (grade[j]<85){
                continue  b;
            list.add(new student(num,grade));
        //Output Results
        for (int i=0;i<Math.min(k, list.size());i++){
            System.out.print(list.get(i)+" ");

Published an original article. Praise 1. Visits 12
Private letter follow

Tags: less Attribute Java

Posted on Fri, 06 Mar 2020 18:43:11 -0800 by eekeek