08-排序3. Talent and Virtue (25)

题目地址:http://www.patest.cn/contests/mooc-ds/08-%E6%8E%92%E5%BA%8F3

根据题目要求 , 编写合适的比较函数,这里先分类,
不同类直接比较
同类别再一步的比较

/* 08-排序3. Talent and Virtue (25) http://www.patest.cn/contests/mooc-ds/08-%E6%8E%92%E5%BA%8F3 */
#include <cstdio> 
#include <cstring> 
#include <algorithm> 
#include <vector>
#include <string>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;

#define N 1002
#define INF 999999999

int n, l, h;

struct mydata
{
    int id;
    int Virtue_Grade;
    int Talent_Grade;
    int totoal_score;
    int rank;
};

vector<mydata> v;

//bool isSages(mydata d)
//{
// if (d.Virtue_Grade >= h && d.Talent_Grade >= h)
// return true;
// return false;
//}
//
//bool isNoblemen(mydata d)
//{
// if (d.Virtue_Grade >= h && d.Talent_Grade < h)
// return true;
// return false;
//}
//
//bool isfoolmen(mydata d)
//{
// if (d.Virtue_Grade < h && d.Talent_Grade <= d.Virtue_Grade)
// return true;
// return false;
//}

int grade(mydata d)
{
    if (d.Virtue_Grade >= h && d.Talent_Grade >= h)
        return 1;
    if (d.Virtue_Grade >= h && d.Talent_Grade < h)
        return 2;
    if (d.Virtue_Grade < h && d.Talent_Grade <= d.Virtue_Grade)
        return 3;
    return 4;
}

bool cmp(mydata dt1, mydata dt2)
{
    int g1 = grade(dt1);
    int g2 = grade(dt2);
    if (g1 < g2)
        return true;
    if (g1 == g2)
    {
        if (dt1.totoal_score > dt2.totoal_score)
            return true;
        if (dt1.totoal_score == dt2.totoal_score)
        {
            if (dt1.Virtue_Grade > dt2.Virtue_Grade)
                return true;
            if (dt1.Virtue_Grade == dt2.Virtue_Grade)
                return dt1.id < dt2.id;
        }
    }
    return false;
}

int main()
{
    //freopen("in", "r", stdin); 
    scanf("%d%d%d", &n, &l, &h);
    int i;
    mydata dt;
    for (i = 0; i < n; i++)
    {
        scanf("%d%d%d", &dt.id, &dt.Virtue_Grade, &dt.Talent_Grade);
        if (dt.Virtue_Grade >= l && dt.Talent_Grade >= l)
        {
            dt.totoal_score = dt.Talent_Grade + dt.Virtue_Grade;
            v.push_back(dt);
        }
    }
    int len = v.size();
    if (len > 1)
        sort(v.begin(), v.end(), cmp);
    printf("%d\n", len);
    for (i = 0; i < len; i++)
    {
        printf("%08d %d %d\n", v[i].id, v[i].Virtue_Grade, v[i].Talent_Grade);
    }
    //printf("\n");
    return 0;
}

你可能感兴趣的:(08-排序3. Talent and Virtue (25))