On virtue and talent of Java algorithm

Posted by silas101 on Fri, 04 Mar 2022 21:09:30 +0100

Title Description

Sima Guang, a historian of the Song Dynasty, has a famous "theory of virtue and talent" in his "Zizhi Tongjian": "therefore, the perfection of talent and virtue is called a saint, the death of talent and virtue is called a fool, the victory of virtue is called a gentleman, and the victory of virtue is called a villain. If you take the skill of man, you can't be a saint. A gentleman and it, rather than a villain, you can't be a fool."

Now we give the scores of virtue and talent of a group of candidates. Please give the admission ranking according to Sima Guang's theory.

input
Input the first line to give three positive integers, which are: N (≤ 105), that is, the total number of candidates; L (≥ 60), which is the lowest score line for admission, that is, candidates whose moral score and talent score are not lower than l are eligible to be considered for admission; H (< 100), is the priority admission line - those whose moral score and talent score are not lower than this line are defined as "full of talent and virtue", and such candidates are ranked from high to low according to the total score of morality and talent; The candidates with less than talent but less than virtue score belong to "virtue wins talent", which is also sorted according to the total score, but they are ranked behind the first category of candidates; Candidates whose moral and talent scores are lower than h, but whose moral scores are not lower than talent scores belong to "both talent and morality" but still have "virtue and talent", which are sorted according to the total score, but ranked behind the second category of candidates; Other candidates who reached the lowest line L were also ranked according to the total score, but ranked behind the third category of candidates.

Then, in line N, each line gives the information of one candidate, including: admission number, German score, in which the admission number is an 8-digit integer, and German score is an integer in the interval [0, 100]. Numbers are separated by spaces

output
The first line of output first gives the number of candidates reaching the lowest score line M, and then M lines. Each line outputs the information of one candidate according to the input format, and the candidates are sorted from high to low according to the rules described in the input. When there are many candidates with the same total score, they are arranged in descending order according to their moral score; If the scores are also in parallel, they will be output in ascending order of the admission number.

Input sample 1

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60
Output sample 1

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90
Input sample 2

5 60 80
100000000 60 70
100000001 80 80
100000002 50 50
100000003 50 90
100000004 60 60
Output sample 2

3
100000000 60 70
100000001 80 80
100000004 60 60
Input sample 3

4 80 80
10000000 80 90
10000001 70 30
10000002 80 70
10000003 100 100
Output sample 3

2
10000000 80 90
10000003 100 100

Problem solving ideas

According to the question, it can be divided into four levels (all according to the total score):
1. Both virtue and talent are higher than H
2. Only when the moral score is greater than L can the score be less than H
3. Moral score and talent score are less than H, and moral score is greater than talent score
4. Both moral and talent scores are greater than L, and moral scores are less than talent scores
Then is the input. You can build a string array and divide it into three parts to store the admission certificate number and German points respectively. Then sort and output in turn.

code

import java.util.*;
import java.util.stream.Collectors;

public class Main {
    //Define a class to store information such as examinee test number and score
    public static class Person implements Comparable<Person>{
        //Admission number
        private final int number;
        //De Fen
        private final int dScore;
        //Talent
        private final int cScore;
        //Total score
        private final int total;

        private Person(int number, int dScore, int cScore){
            this.number = number;
            this.dScore = dScore;
            this.cScore = cScore;
            //The total score is moral score + talent score
            total = dScore + cScore;
        }
        @Override
        public int compareTo(Person o) {
            if(o.total - this.total != 0){
                return o.total - this.total;
            }else if(o.dScore - this.dScore != 0){
                return o.dScore - this.dScore;
            }else{
                return this.cScore - o.cScore;
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //Number of candidates
        int n = sc.nextInt();
        //Minimum admission score
        int l = sc.nextInt();
        //Priority admission line
        int h = sc.nextInt();
        //Store the entered candidate object information
        List<Person> list = new ArrayList<>();
        //Read candidate information
        while(n-- != 0){
            //assignment
            list.add(new Person(sc.nextInt(),sc.nextInt(),sc.nextInt()));
        }
        sc.close();
        //First filter out the candidates whose scores are below the lowest admission score of dScore or cScore
        list = list.stream().filter(v->v.cScore >= l && v.dScore >= l).collect(Collectors.toList());
        //Sort according to t h e descending order of cScore and cScore
        List<Person> one = new ArrayList<>();
        List<Person> two = new ArrayList<>();
        List<Person> three = new ArrayList<>();
        //dScore and cScore are higher than l and lower than h
        List<Person> four = new ArrayList<>();
        //Screen out different categories and sort them according to the given rules
        for(Person p : list){
            //Do your best
            if(p.dScore >= h && p.cScore >= h){
                one.add(p);
            }else if(p.dScore >= h &&  p.cScore < h){
                //Virtue wins talent
                two.add(p);
            }else if(p.dScore <= h && p.cScore <=h && p.dScore >= p.cScore){
                //"Talent and virtue both die", but there are still "virtue wins talent"
                three.add(p);
            }else{
                four.add(p);
            }
        }
        //sort
        Collections.sort(one);
        Collections.sort(two);
        Collections.sort(three);
        Collections.sort(four);
        //total
        System.out.println(one.size()+two.size()+three.size()+four.size());
        //Output in format
        for (Person person : one) {
            System.out.println(person.number + " " + person.dScore + " " + person.cScore);
        }
        for (Person person : two) {
            System.out.println(person.number + " " + person.dScore + " " + person.cScore);
        }
        for (Person person : three) {
            System.out.println(person.number + " " + person.dScore + " " + person.cScore);
        }
        for (Person person : four) {
            System.out.println(person.number + " " + person.dScore + " " + person.cScore);
        }
    }
}

Topics: Java Algorithm