【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
73 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
94 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
86 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
82 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
84 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
89 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
143 0
|
10天前
|
C++ 芯片
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
51 18
|
10天前
|
存储 编译器 数据安全/隐私保护
【C++面向对象——类与对象】CPU类(头歌实践教学平台习题)【合集】
声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,以及两个公有成员函数run、stop。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。​ 相关知识 类的声明和使用。 类的声明和对象的声明。 构造函数和析构函数的执行。 一、类的声明和使用 1.类的声明基础 在C++中,类是创建对象的蓝图。类的声明定义了类的成员,包括数据成员(变量)和成员函数(方法)。一个简单的类声明示例如下: classMyClass{ public: int
37 13
|
10天前
|
编译器 数据安全/隐私保护 C++
【C++面向对象——继承与派生】派生类的应用(头歌实践教学平台习题)【合集】
本实验旨在学习类的继承关系、不同继承方式下的访问控制及利用虚基类解决二义性问题。主要内容包括: 1. **类的继承关系基础概念**:介绍继承的定义及声明派生类的语法。 2. **不同继承方式下对基类成员的访问控制**:详细说明`public`、`private`和`protected`继承方式对基类成员的访问权限影响。 3. **利用虚基类解决二义性问题**:解释多继承中可能出现的二义性及其解决方案——虚基类。 实验任务要求从`people`类派生出`student`、`teacher`、`graduate`和`TA`类,添加特定属性并测试这些类的功能。最终通过创建教师和助教实例,验证代码
37 5