[PAT解题报告] Talent and Virtue
无聊题,题目给了两个指标,德和才(x,y),然后让对一些任根据x, y排序。
又给了两个阈值a<=b,如果x,y都不小于a才人才考虑排序,所以我读入的时候把两个值都小于a的扔掉了 (--i, --n)。
排序原则:
(1) 如果两个指标都不小于b,这个叫做圣人,要排前面
(2) 如果德不小于b,才小于b,这种人叫做君子,排位比圣人稍差,第二位
(3) 两个指标都小于b,并且才不低于德的人叫做愚人,排第三位
(4) 不满足以上条件(可能两个指标都小于b,也可能德小于b,才不小于b),称为小人,排位最后。
同一个级别的人,按照德才的总和排位,较大的排再在前,如果和相同,德大的排在前,否则——德才都相等,只好看id了,id小的排在前。
理解了上述规则旧不难了,关键就是cmp函数,我们先把人分到(1) (2) (3) (4)类别里去,同一类别的再按小规则继续排序……
代码:
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
struct node {
char id[10];
int x,y;
};
node a[100005];
int x,y;
int make(const node &a) {
if (a.x >= y) {
if (a.y >= y) {
return 10000;
}
return 1000;
}
if ((a.y < y) && (a.x >= a.y)) {
return 100;
}
return 10;
}
bool cmp(const node &a,const node &b) {
int ta = make(a), tb = make(b);
if (ta != tb) {
return ta > tb;
}
if (a.x + a.y != b.x + b.y) {
return a.x + a.y > b.x + b.y;
}
if (a.x != b.x) {
return a.x > b.x;
}
return strcmp(a.id, b.id) < 0;
}
int main() {
int n;
scanf("%d%d%d",&n,&x,&y);
for (int i = 0; i < n; ++i) {
scanf("%s%d%d",a[i].id, &a[i].x, &a[i].y);
if ((a[i].x < x) || (a[i].y < x)) {
--i;
--n;
}
}
sort(a, a + n, cmp);
printf("%d\n",n);
for (int i = 0; i < n; ++i) {
printf("%s %d %d\n",a[i].id, a[i].x, a[i].y);
}
return 0;
}
原题链接: http://www.patest.cn/contests/pat-a-practise/1062
查看11道真题和解析
