【pat乙級】1015 德才論

sort排序問題,難度在於條件繁多,其實cmp函數返回的是true,false,就是前面的 a 與 b的關係的判斷。

宋代史學家司馬光在《資治通鑑》中有一段著名的“德才論”:“是故才德全盡謂之聖人,才德兼亡謂之愚人,德勝才謂之君子,才勝德謂之小人。凡取人之術,苟不得聖人,君子而與之,與其得小人,不若得愚人。”

現給出一批考生的德才分數,請根據司馬光的理論給出錄取排名。

輸入格式:

輸入第一行給出 3 個正整數,分別爲:N(≤10​5​​),即考生總數;L(≥60),爲錄取最低分數線,即德分和才分均不低於 L 的考生纔有資格被考慮錄取;H(<100),爲優先錄取線——德分和才分均不低於此線的被定義爲“才德全盡”,此類考生按德才總分從高到低排序;才分不到但德分到線的一類考生屬於“德勝才”,也按總分排序,但排在第一類考生之後;德才分均低於 H,但是德分不低於才分的考生屬於“才德兼亡”但尚有“德勝才”者,按總分排序,但排在第二類考生之後;其他達到最低線 L 的考生也按總分排序,但排在第三類考生之後。

隨後 N 行,每行給出一位考生的信息,包括:准考證號 德分 才分,其中准考證號爲 8 位整數,德才分爲區間 [0, 100] 內的整數。數字間以空格分隔。

輸出格式:

輸出第一行首先給出達到最低分數線的考生人數 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

 思路是分爲四類,放在四個vector當中,這樣按照每個vector輸出即可。

#include<cstdio>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

int H, L;

struct E{
    int sno;
    int dScore;
    int tScore;
}buf[10001];



bool cmp(E a, E b)
{
    int tmp1 = (a.dScore + a.tScore) - (b.dScore + b.tScore);
    int tmp2 = a.dScore - b.dScore;
    int tmp3 = a.sno - b.sno;
    if(tmp1 > 0)
        return true;
    else if(tmp1 == 0)
    {
        if(tmp2 > 0)
        {
            return true;
        }
        else if(tmp2 == 0)
        {
            if(tmp3 < 0)//升序
            {
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int N;
    int a, b, c;
    vector<E> v[4];
    int cnt = 0;
    scanf("%d%d%d", &N, &L, &H);
    for(int i = 0; i < N; i++)
    {
        scanf("%d%d%d", &a, &b, &c);
        if(b >= L && c >= L)
        {
            cnt++;
            E tmp;
            tmp.sno = a;
            tmp.dScore = b;
            tmp.tScore = c;
            if(b >= H && c >= H)
                v[0].push_back(tmp);
            else if(b >= H && c < H)
                v[1].push_back(tmp);
            else if(b < H && c <= b)
                v[2].push_back(tmp);
            else
                v[3].push_back(tmp);
        }
    }
    printf("%d\n", cnt);
    for(int i = 0; i < 4; i++)
    {
        sort(v[i].begin(), v[i].end(), cmp);
        for(int j = 0; j < v[i].size(); j++)
        {
            printf("%d %d %d\n", v[i][j].sno, v[i][j].dScore, v[i][j].tScore);
        }
    }
    return 0;
}

注意tmp的寫法,出現錯誤可能在於第三類,c <= b而不是 c < b.

還有一種暴力的將分類判斷直接寫在cmp函數當中的,但是有點複雜,難度在於cmp函數要對兩個類都進行比較,同樣要注意上面的點可能出錯:

#include<cstdio>
#include<cmath>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

int H, L;

struct E{
    int sno;
    int dScore;
    int tScore;
}buf[100001];

bool judge(int tmp1, int tmp2, int tmp3)
{
    if(tmp1 > 0)
        return true;
    else if(tmp1 == 0)
    {
        if(tmp2 > 0)
        {
            return true;
        }
        else if(tmp2 == 0)
        {
            if(tmp3 < 0)//升序
            {
                return true;
            }
        }
    }
    return false;
}

bool cmp(E a, E b)
{
    int tmp1 = (a.dScore + a.tScore) - (b.dScore + b.tScore);
    int tmp2 = a.dScore - b.dScore;
    int tmp3 = a.sno - b.sno;
    if(a.dScore >= H && a.tScore >= H && b.dScore >= H && b.tScore >= H)
    {
        bool res = judge(tmp1, tmp2, tmp3);
        return res;
    }
    else if((a.dScore >= H && a.tScore >= H))
    {
        return true;;
    }
    else if((b.dScore >= H && b.tScore >= H))
    {
        return false;
    }
    else if((a.dScore >= H && a.tScore < H) && (b.dScore >= H && b.tScore < H))
    {
        bool res = judge(tmp1, tmp2, tmp3);
        return res;
    }
    else if((a.dScore >= H && a.tScore < H))
    {
        return true;
    }
    else if((b.dScore >= H && b.tScore < H))
    {
        return false;
    }
    else if((a.dScore < H && a.tScore <= a.dScore) && (b.dScore < H && b.tScore <= b.dScore))
    {
        bool res = judge(tmp1, tmp2, tmp3);
        return res;
    }
    else if((a.dScore < H && a.tScore <= a.dScore))
    {
        return true;
    }
    else if((b.dScore < H && b.tScore <= b.dScore))
    {
        return false;
    }
    else
    {
        bool res = judge(tmp1, tmp2, tmp3);
        return res;
    }
}

int main()
{
    int N;
    int a, b, c;
    int cnt = 0;
    scanf("%d%d%d", &N, &L, &H);
    for(int i = 0; i < N; i++)
    {
        scanf("%d%d%d", &a, &b, &c);
        if(b >= L && c >= L)
        {
            buf[cnt].sno = a;
            buf[cnt].dScore = b;
            buf[cnt++].tScore = c;
        }
    }
    sort(buf, buf + cnt, cmp);
    printf("%d\n", cnt);
    for(int i = 0; i < cnt; i++)
    {
        printf("%d %d %d\n", buf[i].sno, buf[i].dScore, buf[i].tScore);
    }
    return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章