Header img   

楚虽三户,亡秦必楚



PAT(A) 1062. Talent and Virtue (25)

原题目:

原题链接:https://www.patest.cn/contests/pat-a-practise/1062

1062. Talent and Virtue (25)


About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage(圣人)"; being less excellent but with one's virtue outweighs talent can be called a "nobleman(君子)"; being good in neither is a "fool man(愚人)"; yet a fool man is better than a "small man(小人)" who prefers talent than virtue. Now given the grades of talent and virtue of a group of people, you are supposed to rank them according to Sima Guang's theory.

Input Specification:

Each input file contains one test case. Each case first gives 3 positive integers in a line: N (<=105), the total number of people to be ranked; L (>=60), the lower bound of the qualified grades -- that is, only the ones whose grades of talent and virtue are both not below this line will be ranked; and H (<100), the higher line of qualification -- that is, those with both grades not below this line are considered as the "sages", and will be ranked in non-increasing order according to their total grades. Those with talent grades below H but virtue grades not are cosidered as the "noblemen", and are also ranked in non-increasing order according to their total grades, but they are listed after the "sages". Those with both grades below H, but with virtue not lower than talent are considered as the "fool men". They are ranked in the same way but after the "noblemen". The rest of people whose grades both pass the L line are ranked after the "fool men".

Then N lines follow, each gives the information of a person in the format:

ID_Number Virtue_Grade Talent_Grade where ID_Number is an 8-digit number, and both grades are integers in [0, 100]. All the numbers are separated by a space. Output Specification:

The first line of output must give M (<=N), the total number of people that are actually ranked. Then M lines follow, each gives the information of a person in the same format as the input, according to the ranking rules. If there is a tie of the total grade, they must be ranked with respect to their virtue grades in non-increasing order. If there is still a tie, then output in increasing order of their ID's.

Sample Input: 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 Sample Output: 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

题目大意

本题题不难,关键看理解。 1. 首行给出三个数字,N(人数),L(最低标准),H(最高标准)。 1. 后N行,每行分别为人员编码,品德分值,才华分值。 1. 品德分值或才华分值任意低于L的不予收录。 1. 品德分值或才华分值都不小于H的为sale圣人。 1. 品德分不小于H,但才华分低于H的为nobleman君子。 1. 品德分与才华分都低于H,但品德分高于才华分的为foolman愚人。 1. 剩下的便是其他了。 1. 先输出圣人,其后君子、愚人、其他人,同类人以总分高为序,总分一样以品德高为序,品德一样以编码低为序。

解题报告

解题不难,难在坑点。 1. 理解排序方案,转化为代码。 1. 时间有限,故使用一个vector存储,在结构体中以level表示级别。 1. 上两个解决了不知为何仍不过,加std::ios::sync_with_stdio(false);通过。

代码

/*
* Problem: 1062. Talent and Virtue (25)
* Author: HQ
* Time: 2018-03-07
* State: Done
* Memo:
*/
#include "iostream"
#include "string"
#include "algorithm"
#include "vector"
using namespace std;

struct Person {
    string num;
    int virtue;
    int talent;
    int total;
    int level;
};
int N, L, H;
vector<Person> person;

int cmp(const Person &a, const Person &b) {
    if (a.level != b.level)
        return a.level > b.level;
    if (a.total != b.total)
        return a.total > b.total;
    if (a.virtue != b.virtue)
        return a.virtue > b.virtue;
    return a.num < b.num;
}

int main() {
    std::ios::sync_with_stdio(false);
    cin >> N >> L >> H;
    for (int i = 0; i < N; i++) {
        Person temp;
        cin >> temp.num >> temp.virtue >> temp.talent;
        if (temp.virtue < L || temp.talent < L)
            continue;
        else {
            temp.total = temp.virtue + temp.talent;
            if (temp.virtue >= H && temp.talent >= H)
                temp.level = 4;
            else if (temp.virtue >= H && temp.talent < H)
                temp.level = 3;
            else if (temp.virtue < H && temp.talent < H && temp.virtue >= temp.talent)
                temp.level = 2;
            else
                temp.level = 1;
            person.push_back(temp);
        }
    }
    sort(person.begin(), person.end(), cmp);
    cout << person.size() << endl;
    for (int i = 0; i < person.size(); i++) {
        cout << person[i].num << " " << person[i].virtue << " " << person[i].talent << endl;
    }
    system("pause");
}


评论0

发表评论