Java: P1093 [NOIP2007 popularization group] scholarship

Keywords: Java Algorithm Back-end

Luogu topic: P1093 [NOIP2007 popularization group] scholarship

Title Description

A primary school has recently received a sponsorship and plans to give part of it to the top five students with excellent academic achievements. At the end of the term, each student has three grades: Chinese, mathematics and English. First, sort according to the total score from high to low. If the total scores of the two students are the same, then sort according to the Chinese scores from high to low. If the total scores and Chinese scores of the two students are the same, the students with small student number are required to be in the front. In this way, the ranking of each student is uniquely determined.

Task: first calculate the total score according to the scores of the three courses entered, then sort according to the above rules, and finally output the student number and total score of the top five students according to the ranking order. Note that the scholarships of each of the top five students are different, so you must sort them strictly according to the above rules. For example, in a correct answer, if the output data of the first two lines (two numbers per line: student number and total score) is:

77 279
55 279

The meaning of these two lines of data is that the student numbers of the two students with the highest total scores are 77 and 55 in turn. The total score of the two students is 279279 (the total score is equal to the sum of the input scores of Chinese, mathematics and English), but the Chinese score of the student with student number 77 is higher. If your top two output data is:

55 279
77 279

It is treated as an output error and cannot be scored.

Input format

n+1 lines in total.

Line 11 is a positive integer n (\ Le 300) n (≤ 300), indicating the number of students participating in the selection.

Lines 22 to n+1n+1 have 33 numbers separated by spaces, each between 00 and 100100. The 33 numbers in the jj line represent the scores of students with student number j-1j − 1 in Chinese, mathematics and English. The student number of each student is numbered as 1~n1 n (exactly the line number of the input data minus 11).

The data given are correct and do not need to be tested.

Output format

There are 5 lines in total, and each line is two positive integers separated by spaces, representing the student number and total score of the top 55 students in turn.

Input and output samples

Test point 1:

6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98
6 265
4 264
3 258
2 244
1 237

Test point 2:

8
80 89 89
88 98 78
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98
8 265
2 264
6 264
1 258
5 258

My code

import java.util.Arrays;
import java.util.Scanner;
/*
 * Idea: use the Comparable interface, implement the compareTo () method at the same time, and then change it to descending order,
 * Finally, the first five are output.
 */
public class Main{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Cj[] cjs = new Cj[n];
        for(int i = 0;i<cjs.length;i++)
        {
            // Read data
            cjs[i] = new Cj(in.nextInt(),in.nextInt(),in.nextInt(),i+1);
        }
        Arrays.sort(cjs);                   // Built in quick sort
        for(int i = 0;i<5;i++)              // Output the first five
        {
            System.out.println(cjs[i]);
        }
    }
}
// Remember to implement the Comparable interface and write the class name of the Comparable interface in < >
class Cj implements Comparable<Cj>{
    int no;                 // Because the student number needs to be output finally, it is natural to make a no variable
    int y;                  // grade scores of Chinese
    int s;                  // Mathematics achievement
    int e;                  // English achievement
    int z;                  // Total score

    public Cj() {

    }
    public Cj(int y,int s,int e,int no) {
        this.y = y;
        this.s = s;
        this.e = e;
        z = y+s+e;
        this.no = no;
    }

    // The following methods are based on Arrays.sort(), and the output is the method written out from the previous data
    public int compareTo(Cj x) {
        if(this.z == x.z)
        {
            if(this.y == x.y)
            {
                return this.no - x.no;  // If you want a small one in front, this. Variable - reference. Variable
            }
            return x.y - this.y;        // If you want a large one in front, reference the. Variable - this. Variable
        }
        return x.z - this.z;            // If you want a large one in front, reference the. Variable - this. Variable
    }
    
    // When outputting, I override the toString () method, so in Main, the output object is directly retrieved automatically
    // The toString () method of the object.
    public String toString() {
        return no+" "+z;
    }

}

Post question summary

1, From this, we can conclude that if you want to sort an array, the Arrays.sort() function can only solve the array of basic data types. If you want to sort an array of reference types, you need to implement the Comparable interface (don't forget that after the interface, you need < class name of the interface >) and rewrite the compareTo() method.

2, For the rewriting of compareTo() method, if you want the large one in front of the array, reference the. Variable - this. Variable
                    if you want the small array to be in front of the array, this. Variable - reference. Variable

Posted by miles_rich on Tue, 09 Nov 2021 17:15:22 -0800