PAT1 1062 Talent and Virtue

题目链接
我的github

题目大意

现在给出一些学生的id,品德分和才能分,要求将这些学生排序

  • 参与排序学生的品德分和才能分必须都达到最低要求
  • 如果品德分和才能分都大于等于所给的高分就判定为圣人
  • 如果品德分大于等于所给高分大于才能分就判定为君子
  • 如果品德分和才能分都低于所给高分,但是品德分大于等于才能分就判定为庸人
  • 其他就判定为小人
  • 排序规则:先有圣人>君子>庸人>小人,然后总分高的>总分低的, 之后品德分高的>品德分低的,最后是id小>id大的

输入

每组包含一个测试用例

  • 第一行是三个正整数 N ≤ 1 0 5 N\leq10^5 N105表示学生总数, L ≥ 60 L\ge60 L60表示最低要求的分数, H &lt; 100 H&lt;100 H<100表示所给高分
  • 之后有 N N N行,每行格式为ID_Number Virtue_Grade Talent_GradeID_Number是一个八位数,所有的分数都在 [ 0 , 100 ] [0,100] [0,100]之间

输出

对每个测试用例,先在一行中输出 M M M表示共有多少人参与排序
然后有 M M M行输出排序后的学生,格式与输入一致

样例输入

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

解析

按照题意将每个学生归类,然后在排序函数里面写好排序规则进行排序即可
python会超时
C++AC
python:

# -*- coding: utf-8 -*- 
# @Time : 2019/6/24 13:57 
# @Author : ValarMorghulis 
# @File : 1062.py
import functools


class node:
    def __init__(self, id, virtue, talent, tot, category):
        self.id = id
        self.virtue = virtue
        self.talent = talent
        self.tot = tot
        self.category = category

    def toString(self):
        return "%s %d %d" % (self.id, self.virtue, self.talent)


def cmp(a, b):
    if a.category != b.category:
        return -1 if a.category < b.category else 1
    if a.tot != b.tot:
        return -1 if a.tot > b.tot else 1
    if a.virtue != b.virtue:
        return -1 if a.virtue > b.virtue else 1
    return -1 if a.id < b.id else 1


def solve():
    n, low, high = map(int, input().split())
    a = list()
    for i in range(n):
        id, virtue, talent = input().split()
        virtue, talent = int(virtue), int(talent)
        if virtue >= low and talent >= low:
            if virtue >= high and talent >= high:
                a.append(node(id, virtue, talent, virtue + talent, 1))
            elif virtue >= high > talent:
                a.append(node(id, virtue, talent, virtue + talent, 2))
            elif high > virtue >= talent and talent < high:
                a.append(node(id, virtue, talent, virtue + talent, 3))
            else:
                a.append(node(id, virtue, talent, virtue + talent, 4))
    a=sorted(a, key=functools.cmp_to_key(cmp))
    print(len(a))
    for i in a:
        print(i.toString())


if __name__ == "__main__":
    solve()

C++:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<cmath>

#define inf 0xffffffff

using namespace std;

typedef struct node
{
    int id, virtue, talent, tot, category;
};

bool cmp(node a, node b)
{
    if(a.category!=b.category)
        return a.category<b.category;
    if(a.tot!=b.tot)
        return a.tot>b.tot;
    if(a.virtue!=b.virtue)
        return a.virtue>b.virtue;
    return a.id<b.id;
}

int main()
{
    int n, low, high;
    scanf("%d%d%d", &n, &low, &high);
    vector<node> a;
    for(int i=0; i<n; i++)
    {
        int id, virtue, talent;
        scanf("%d%d%d", &id, &virtue, &talent);
        if(virtue>=low&&talent>=low)
        {
            if(virtue>=high&&talent>=high)
            {
                node t;
                t.id=id, t.virtue=virtue, t.talent=talent, t.tot=virtue+talent, t.category=1;
                a.push_back(t);
            }
            else
                if(virtue>=high&&talent<high)
                {
                    node t;
                    t.id=id, t.virtue=virtue, t.talent=talent, t.tot=virtue+talent, t.category=2;
                    a.push_back(t);
                }
                else
                    if(virtue<high&&talent<high&&talent<=virtue)
                    {
                        node t;
                        t.id=id, t.virtue=virtue, t.talent=talent, t.tot=virtue+talent, t.category=3;
                        a.push_back(t);
                    }
                    else
                    {
                        node t;
                        t.id=id, t.virtue=virtue, t.talent=talent, t.tot=virtue+talent, t.category=4;
                        a.push_back(t);
                    }

        }
    }
    int cnt=a.size();
    sort(a.begin(), a.end(), cmp);
    printf("%d\n", cnt);
    for(int i=0; i<cnt; i++)
        printf("%d %d %d\n", a[i].id, a[i].virtue, a[i].talent);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值