【PAT甲级 - C++题解】1062 Talent and Virtue

简介: 【PAT甲级 - C++题解】1062 Talent and Virtue

1062 Talent and Virtue


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 considered 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


题意

给定每个人的信息,分别为编号 id ,德行分数 virtue 和才华分数 talent 。


现在需要我们对给定的人进行排名,排名规则如下:


1.才华和德行均不低于 H 的是圣人,圣人们按德才总分不升序排序。

2.才华低于 H ,德行不低于 H 的是君子,君子们按德才总分不升序排序,他们都低于圣人。

3.才华和德行均低于 H ,但是德行不低于才华的是愚人,愚人们按德才总分不升序排序,他们都低于君子。

4.能够参与排名的,不属于以上三类人的均是小人,小人们按德才总分不升序排序,他们都低于愚人。


思路

具体思路如下:


1.可以用一个结构体来存储每一个人的信息,并制定好排序规则。

2.输入每个人的信息,德才分数均低于 L 的人不参与排名。可以将参与排名的人按照排名规则划分成四个等级,结构体中的排序规则就可以利用获得的等级进行排序。

3.对参与排名的人进行排序,最后输出排序后的结果。


代码

#include<bits/stdc++.h>
using namespace std;
int n, H, L;
const int N = 100010;
struct Person
{
    int id, virtue, talent, level;
    //计算总分
    int total() const
    {
        return virtue + talent;
    }
    //排名规则
    bool operator<(const Person& t)const
    {
        if (level != t.level)  return level < t.level;
        else if (total() != t.total()) return total() > t.total();
        else if (virtue != t.virtue)   return virtue > t.virtue;
        else return id < t.id;
    }
}p[N];
int main()
{
    cin >> n >> L >> H;
    int m = 0;
    for (int i = 0; i < n; i++)
    {
        //输入每个人的信息
        int id, virtue, talent;
        cin >> id >> virtue >> talent;
        //如果不满足排名要求则直接跳过该人
        if (virtue < L || talent < L)  continue;
        //进行等级划分
        int level;
        if (virtue >= H && talent >= H)        level = 1;
        else if (virtue >= H && talent < H)    level = 2;
        else if (virtue >= talent)         level = 3;
        else                            level = 4;
        //存入该人信息
        p[m++] = { id,virtue,talent,level };
    }
    //进行排序
    sort(p, p + m);
    //输出最终排名
    cout << m << endl;
    for (int i = 0; i < m; i++)
        printf("%08d %d %d\n", p[i].id, p[i].virtue, p[i].talent);
    return 0;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
61 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
77 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
70 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
69 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
74 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
73 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
121 0
|
15天前
|
编译器 C++
C++ 类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
60 30
|
4天前
|
并行计算 Unix Linux
超级好用的C++实用库之线程基类
超级好用的C++实用库之线程基类
12 4
|
4天前
|
C++ Windows
HTML+JavaScript构建C++类代码一键转换MASM32代码平台
HTML+JavaScript构建C++类代码一键转换MASM32代码平台