当前位置: 代码迷 >> 综合 >> [PAT B1015/A1062]Talent and Virtue
  详细解决方案

[PAT B1015/A1062]Talent and Virtue

热度:34   发布时间:2023-12-15 06:22:17.0

[PAT B1015/A1062]Talent and Virtue

本题是甲级1062和乙级1015题,所以先放英文题目,如果是要做乙级题的话,可以不看英文,直接看下面的中文题

题目描述

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.

输入格式

Each input file contains one test case. Each case first gives 3 positive integers in a line: N (≤10?5??), 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.

输出格式

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.

输入样例

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

输出样例

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

--------------------------中文原题---------------------------

题目描述

1015 德才论 (25 分)宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”
现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式

输入第一行给出 3 个正整数,分别为:N(≤10?5??),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。
随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

解析

这个题目很简单,就是我的英语水平不好,看题目看了半天还是没看透彻,导致花费了一些时间。另外最最最重要的一点就是:使用printf()而不是cout,我第一次做这道题目的时候就是使用的cout结果导致有两组测试点超时通不过,结果只有19分,想一想,朋友,把cout改成printf,只需要一点点的修改(printf运行时间比cout要短得多),可以直接加6分,所以今后的编程中我用printf了,你随意哈哈(开个玩笑,还是希望大家都用printf)
下面贴代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct person
{
    int id;int v_grade;int t_grade;
};
bool cmp(person p1, person p2)
{
    int p1_total = p1.t_grade + p1.v_grade, p2_total = p2.t_grade + p2.v_grade;if(p1_total!=p2_total) return (p1.t_grade + p1.v_grade) > (p2.t_grade + p2.v_grade);else if (p1.v_grade != p2.v_grade) return p1.v_grade > p2.v_grade;else return p1.id < p2.id;
}
int main()
{
    vector<person> sages;vector<person> noblemen;vector<person> foolmen;vector<person> smallmen;int N, L, H;cin >> N >> L >> H;while (N--){
    person p;cin >> p.id >> p.v_grade >> p.t_grade;if (p.t_grade >= L && p.v_grade >= L) {
    if (p.t_grade >= H && p.v_grade >= H) sages.push_back(p);else if (p.t_grade < H && p.v_grade >= H) noblemen.push_back(p);else if (p.t_grade < H &&p.v_grade < H &&p.v_grade >= p.t_grade) foolmen.push_back(p);else smallmen.push_back(p);}}sort(sages.begin(), sages.end(), cmp);sort(noblemen.begin(), noblemen.end(), cmp);sort(foolmen.begin(), foolmen.end(), cmp);sort(smallmen.begin(), smallmen.end(), cmp);cout << sages.size() + noblemen.size() + foolmen.size() + smallmen.size() << endl;for (int i = 0; i < sages.size(); i++) printf("%d %d %d\n", sages[i].id, sages[i].v_grade, sages[i].t_grade);for (int i = 0; i < noblemen.size(); i++) printf("%d %d %d\n", noblemen[i].id, noblemen[i].v_grade, noblemen[i].t_grade);for (int i = 0; i < foolmen.size(); i++) printf("%d %d %d\n", foolmen[i].id, foolmen[i].v_grade, foolmen[i].t_grade);for (int i = 0; i < smallmen.size(); i++) printf("%d %d %d\n", smallmen[i].id, smallmen[i].v_grade, smallmen[i].t_grade);return 0;
}

水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出宝贵的意见!