PAT乙級.1015. 德才論 (25)

1015. 德才論 (25)


題目

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

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

輸入格式

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

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

輸出格式

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

PAT鏈接


思路

1.用struct存儲信息,用其中的rank來對成績分類

struct Student
{
    int id; //由於題目說是8位,int恰好夠用
    int moral;
    int ability;
    int rank;   //1~4   用來分類
}stu[Max + 10], tmp;

2.依次讀取每條成績,過濾未達標的成績,並將每個有效成績的rank求出來
3.調用STL的sort()函數來進行排序,並按要求設置cmp函數


代碼

/**
* @tag     PAT_B_1015
* @authors R11happy (xushuai100@126.com)
* @date    2016-9-1 16:54-17:32
* @version 1.0
* @Language C++
* @Ranking  480/1259
* @function null
*/

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

const int Max = 100000;

struct Student
{
    int id; //由於題目說是8位,int恰好夠用
    int moral;
    int ability;
    int rank;   //1~4   用來分類
}stu[Max + 10], tmp;

bool cmp(Student a, Student b)
{
    if(a.rank != b.rank)    return a.rank < b.rank;
    int sumA = a.moral+a.ability;
    int sumB = b.moral+b.ability;
    if(sumA != sumB)    return sumA > sumB;
    else if(a.moral != b.moral) return a.moral > b.moral;
    else    return a.id < b.id;
}

int main(int argc, char const *argv[])
{
    int N, M = 0;
    int L_grade, H_grade;
    scanf("%d%d%d", &N, &L_grade, &H_grade);
    while (N--)
    {
        scanf("%d %d %d", &tmp.id, &tmp.moral, &tmp.ability);
        if (tmp.moral >= L_grade && tmp.ability >= L_grade)
        {
            if(tmp.moral >= H_grade && tmp.ability >= H_grade)
            {
                tmp.rank = 1;   //才德全盡
            }
            else if(tmp.moral >= H_grade && tmp.ability < H_grade)
            {
                tmp.rank = 2;   //徳勝才
            }
            else if(tmp.moral < H_grade && tmp.ability < H_grade && tmp.moral >= tmp.ability)
            {
                tmp.rank = 3;   //才德兼亡
            }
            else    tmp.rank = 4;
            stu[M++] = tmp;
        }
    }
    sort(stu, stu+M, cmp);  //按cmp規則排序
    printf("%d\n", M );
    for(int i = 0; i<M; i++)
    {
        printf("%d %d %d\n",stu[i].id, stu[i].moral, stu[i].ability );
    }
    return 0;
}

收穫

1.對先分類,再在每組中排序的情況:在struct中設置一個rank
2.調用STL中的sort()函數,以及cmp函數的書寫

bool cmp(Student a, Student b)
{
    if(a.rank != b.rank)    return a.rank < b.rank; //各組升序
    int sumA = a.moral+a.ability;
    int sumB = b.moral+b.ability;
    if(sumA != sumB)    return sumA > sumB; //總分降序
    else if(a.moral != b.moral) return a.moral > b.moral;   //徳分降序
    else    return a.id < b.id; //id升序
}

3.本題由於說明id最多8位,故可以用int類型來存
但如果位數再多,如char id[10]的情況,在進行比較可用

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