PAT (Basic Level) Practice (Chinese) 1015 theory of virtue and talent (25 points)

Posted by Buddha443556 on Mon, 20 Dec 2021 05:32:19 +0100

This problem is not difficult. Just figure out the progressive sorting relationship and use the sort function. This problem tells us that if you can write c + + with vector, you don't use {array, and if you can use struct, you should use struct

Original question:

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:

The first line of input gives three positive integers, They are: N (≤ 105), i.e. the total number of candidates; l (≥ 60), which is the lowest score line for admission, i.e. candidates whose moral score and talent score are not lower than ﹤ L ﹤ are eligible for admission; H (< 100), which 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" , such candidates are ranked from high to low according to the total score of virtue 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; 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 wins talent", which are sorted according to the total score, but ranked behind the second category of candidates; Other candidates who have reached the lowest line of , L , are also ranked according to the total score, but they are ranked behind the third category of candidates.

Then, in line N, 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, 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 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

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

 

Code:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct student {
	int n;
	int d;
	int c;
    int cla;
    int sum;
};
bool cmp(const student &a,const student &b){
    if (a.cla!=b.cla) return a.cla<b.cla;
    else if (a.sum != b.sum) return a.sum>b.sum;
    else if (a.d != b.d) return a.d>b.d;
    else return a.n<b.n;
}
int main() {
	int number, de, cai;
	int num, dcmin, dcmid;
    int ji = 0;
	student s;
	vector<student> lis;
	cin >> num >> dcmin >> dcmid;
	while (cin >> number >> de >> cai) {
		s.n = number;
		s.d = de;
		s.c = cai;
		lis.push_back(s);
	}
    for(int i = 0;i<num;i++)
    {
        lis[i].sum = lis[i].d+lis[i].c;
        if(lis[i].d>=dcmin && lis[i].c>=dcmin)
            ji++;
        if(lis[i].d>=dcmid && lis[i].c>=dcmid)
            lis[i].cla = 1;
        else if(lis[i].d<dcmin || lis[i].c<dcmin)
            lis[i].cla = 5;
        else if(lis[i].d>=dcmid && lis[i].c < dcmid)
            lis[i].cla = 2;
        else if(lis[i].d>=lis[i].c)
            lis[i].cla = 3;
        else
            lis[i].cla = 4;
    }
    cout << ji << endl;
        sort(lis.begin(),lis.end(),cmp);
    for(int i = 0;i<num;i++)
        if(lis[i].cla<5)
    cout << lis[i].n<<" "<<lis[i].d<<" "<<lis[i].c <<endl;
}

Topics: C++ pta