【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
66 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
82 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
77 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
76 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
79 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
80 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
136 0
|
6天前
|
存储 编译器 C++
【c++】类和对象(下)(取地址运算符重载、深究构造函数、类型转换、static修饰成员、友元、内部类、匿名对象)
本文介绍了C++中类和对象的高级特性,包括取地址运算符重载、构造函数的初始化列表、类型转换、static修饰成员、友元、内部类及匿名对象等内容。文章详细解释了每个概念的使用方法和注意事项,帮助读者深入了解C++面向对象编程的核心机制。
30 5
|
13天前
|
存储 编译器 C++
【c++】类和对象(中)(构造函数、析构函数、拷贝构造、赋值重载)
本文深入探讨了C++类的默认成员函数,包括构造函数、析构函数、拷贝构造函数和赋值重载。构造函数用于对象的初始化,析构函数用于对象销毁时的资源清理,拷贝构造函数用于对象的拷贝,赋值重载用于已存在对象的赋值。文章详细介绍了每个函数的特点、使用方法及注意事项,并提供了代码示例。这些默认成员函数确保了资源的正确管理和对象状态的维护。
41 4
|
14天前
|
存储 编译器 Linux
【c++】类和对象(上)(类的定义格式、访问限定符、类域、类的实例化、对象的内存大小、this指针)
本文介绍了C++中的类和对象,包括类的概念、定义格式、访问限定符、类域、对象的创建及内存大小、以及this指针。通过示例代码详细解释了类的定义、成员函数和成员变量的作用,以及如何使用访问限定符控制成员的访问权限。此外,还讨论了对象的内存分配规则和this指针的使用场景,帮助读者深入理解面向对象编程的核心概念。
40 4