pat(A) 1062. Talent and Virtue(结构体排序)

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
using namespace std;

struct Node
{
    int num;
    int v;
    int t;
    void Set(int x,int y,int z)
    {
        num=x;
        v=y;
        t=z;
    }
};

int cmp(Node a,Node b)
{
    if((a.t+a.v==b.v+b.t)&&(a.v==b.v))
    {
        return a.num<b.num;
    }
    else if(a.t+a.v==b.v+b.t)
    {
        return a.v>b.v;
    }
    else
    {
        return (a.t+a.v)>(b.v+b.t);
    }
}

Node a[N];
Node b[N],c[N],d[N];

int main()
{
    int n,l,h;
    while(scanf("%d%d%d",&n,&l,&h)==3)
    {
        int len[5]={0};
        for(int i=0;i<n;i++)
        {
            int num,v,t;
            scanf("%d%d%d",&num,&v,&t);
            if(v>=h&&t>=h)
            {
                a[len[1]++].Set(num,v,t);
            }
            else if(v>=h&&t<h&&t>=l)
            {
                b[len[2]++].Set(num,v,t);
            }
            else if(v>=l&&t>=l&&v<h&&t<h&&v>=t)
            {
                c[len[3]++].Set(num,v,t);
            }
            else if(v>=l&&t>=l)
            {
                d[len[4]++].Set(num,v,t);
            }
        }
        //printf("-----------------------\n");
        printf("%d\n",len[1]+len[2]+len[3]+len[4]);
        sort(a,a+len[1],cmp);
        sort(b,b+len[2],cmp);
        sort(c,c+len[3],cmp);
        sort(d,d+len[4],cmp);
        for(int i=0;i<len[1];i++)
        {
            printf("%08d %d %d\n",a[i].num,a[i].v,a[i].t);
        }
        for(int i=0;i<len[2];i++)
        {
            printf("%08d %d %d\n",b[i].num,b[i].v,b[i].t);
        }
        for(int i=0;i<len[3];i++)
        {
            printf("%08d %d %d\n",c[i].num,c[i].v,c[i].t);
        }
        for(int i=0;i<len[4];i++)
        {
            printf("%08d %d %d\n",d[i].num,d[i].v,d[i].t);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-16 21:59:44

pat(A) 1062. Talent and Virtue(结构体排序)的相关文章

1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise

题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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 i

PAT 1062 Talent and Virtue[难]

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(圣

PAT 1062. Talent and Virtue (25)

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(圣人

PAT B1015/A1062 Talent and Virtue

PAT B1015/A1062 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

【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能讲讲其用法: 1.sort入门: 使用sort需要包含algorithm头文件,完整代码如下 #include<iostream> #include<vector> #include<algorithm>//貌似可以不用,但最好加上. using namespace std

HDU 1084 [What Is Your Grade?] 结构体排序

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1084 题目大意:共5道题,N个学生.做出5道满分,0道50分.做出1-4道的同学,若在前50%(向下取整),则获得95.85.75.65,若在后50%则获得90.80.70.60. 关键思想:结构体排序 //结构体排序,考虑边界 #include <iostream> #include <algorithm> #include <cmath> #include <me

C++结构体排序

在C++中,对结构体的排序方式比C语言丰富的多.在C语言中,我们主要是通过qsort进行排序操作(抛开手写排序算法不说). 在C++<algorithm>中,有一个十分强大的排序函数sort,他的内部综合了许多种排序算法,因此非常高效.并且,用它来对结构体排序也十分方便. 先贴一段示例代码: 1 #include <cstdio> 2 #include <queue> 3 #include <vector> 4 #include <algorithm&

结构体排序

经常碰到结构体排序的问题,在此总结一下.以一个简单的例题开始: 例1.有三个人(Person结构体),每个人都有name(string型)和age(int型)两个属性,现在需要按照下面的规则排序:先以姓名按从小到大排序(如abc<abd),如果姓名相同,则按照年龄从大到小排序. #include<iostream> #include<string> using namespace std; struct Person{ string name; int age; }; voi

HDOJ 1009. Fat Mouse&#39; Trade 贪心 结构体排序

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 56784    Accepted Submission(s): 19009 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g