PAT class B question brushing / 1015 theory of virtue and talent / C + + implementation

Posted by astaroth on Wed, 01 Dec 2021 07:02:42 +0100

1, Title Description

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

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

Input format:

Input the first line and give 3 positive integers, respectively: N (≤ 105), that is, the total number of candidates; L (≥ 60) is the lowest score line for admission, that is, both moral score and talent score are not lower than   L   Candidates 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 who can't get the talent score but get the moral score line belong to "virtue wins talent", which is also sorted according to the total score, but they are ranked behind the first category of candidates; Both moral and talent scores are lower than   H. However, candidates whose moral score is not lower than that of talent belong to "both talent and morality" but still have "virtue wins talent", which are sorted according to the total score, but ranked behind the second category of candidates; Others reach the lowest line   L   Of candidates are also ranked according to the total score, but behind the third category of candidates.

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

Output format:

The first line of output first gives the number of candidates who have reached the lowest score line   M. Then   M   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 example:

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
//No blank lines at the end

Output example:

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
//No blank lines at the end

2, Train of thought analysis

After reading the questions, we found that the most basic requirement is that both moral and talent scores need to reach the passing score before they can be admitted. On this basis, they are divided into four categories of candidates.

Class I: moral and talent scores are not lower than the priority admission line

Class II: the moral score is not lower than the priority admission line, but the score is not reached

The third category: the moral and talent scores did not reach the priority admission line, but the moral score was higher than the talent score

Category IV: other remaining candidates

After clarifying the above classification, four structural dynamic arrays can be established. The relevant information of the four types of candidates can be added to the array by using the for loop and if else conditional control statement, sorted by using the sort() function, and then output.

3, Problem solving code and submission screenshot

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct StuInfo
{
    int StuId;
    int DeScore;
    int CaiScore;
    int sum;
};

bool cmp(StuInfo a,StuInfo b);

int main()
{
    int N,H,L;
    cin >> N >> L >> H;
    vector<StuInfo> list_1;//storage
    vector<StuInfo> list_2;
    vector<StuInfo> list_3;
    vector<StuInfo> list_4;
    for(int i = 0;i < N;i++){
        int StuId,DeScore,CaiSore;
        cin >> StuId >> DeScore >> CaiSore;
        StuInfo student{};
        if(DeScore >= L && CaiSore >= L){ //Basic admission requirements
            student.StuId = StuId;
            student.DeScore = DeScore;
            student.CaiScore = CaiSore;
            student.sum = DeScore + CaiSore;
            if(DeScore >= H && CaiSore >= H){  //first kind
                list_1.push_back(student);
            }
            else if(CaiSore < H && DeScore >= H){  //Type II
                list_2.push_back(student);
            }
            else if(CaiSore < H && DeScore < H && DeScore >= CaiSore){  //Category III
                list_3.push_back(student);
            }
            else{
                list_4.push_back(student);  //Category IV
            }
        }

    }
    sort(list_1.begin(),list_1.end(),cmp); //Sort by cmp
    sort(list_2.begin(),list_2.end(),cmp);
    sort(list_3.begin(),list_3.end(),cmp);
    sort(list_4.begin(),list_4.end(),cmp);
    cout << list_1.size() + list_2.size() + list_3.size() + list_4.size() << endl;
   
 for(auto & j : list_1){ //output
        cout << j.StuId
             << " "
             << j.DeScore
             << " "
             << j.CaiScore
             << endl;
    }
    for(auto & j : list_2){
        cout << j.StuId
             << " "
             << j.DeScore
             << " "
             << j.CaiScore
             << endl;
    }
    for(auto & j : list_3){
        cout << j.StuId
             << " "
             << j.DeScore
             << " "
             << j.CaiScore
             << endl;
    }
    for(auto & j : list_4){
        cout << j.StuId
             << " "
             << j.DeScore
             << " "
             << j.CaiScore
             << endl;
    }
}

bool cmp(StuInfo a,StuInfo b) //cmp function
{
    if(a.sum != b.sum){
        return a.sum > b.sum;
    }
    else if(a.DeScore != b.DeScore){
        return a.DeScore > b.DeScore;
    }
    else{
        return a.StuId < b.StuId;
    }
}

 

 

4, Summary

The sort() function plays an important role. Specific usage:

#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
#include <string_view>
 
int main()
{
    std::array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
 
    auto print = [&s](std::string_view const rem) {
        for (auto a : s) {
            std::cout << a << ' ';
        }
        std::cout << ": " << rem << '\n';
    };
 
    std::sort(s.begin(), s.end());
    print("sorted with the default operator<");
 
    std::sort(s.begin(), s.end(), std::greater<int>());
    print("sorted with the standard library compare function object");
 
    struct {
        bool operator()(int a, int b) const { return a < b; }
    } customLess;
    std::sort(s.begin(), s.end(), customLess);
    print("sorted with a custom function object");
 
    std::sort(s.begin(), s.end(), [](int a, int b) {
        return a > b;
    });
    print("sorted with a lambda expression");
}
0 1 2 3 4 5 6 7 8 9 : sorted with the default operator<
9 8 7 6 5 4 3 2 1 0 : sorted with the standard library compare function object
0 1 2 3 4 5 6 7 8 9 : sorted with a custom function object
9 8 7 6 5 4 3 2 1 0 : sorted with a lambda expression

 

Topics: C++ Algorithm array