PAT乙级1015

1015 德才论 (25分)

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第一行给出 3 个正整数,分别为:N(≤105),即考生总数;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

我的理解

《资治通鉴》,想读而未读的一本书,呵呵。个人感觉这个题目的分类跟司马光的“德才论”还是有些出入的。

分类总共分四类,暂称I类,II类,III类,IV类。分类之后还要进行排序,而排序的规则又是“嵌套”的那种。可我连个排序都写不好、、、可以先进行分类,分类到四个数组中,然后各自进行排序。录取要保证德分和才分均大于等于最低录取线。

  1. I类。德分和才分均大于等于优先录取线。
  2. II类。德分大于等于优先录取线,并且才分大于等于最低录取线,才分小于优先录取线。
  3. III类。德分,才分均大于等于最低录取线,小于优先录取线,并且德分大于等于才分。
  4. IV类。其余剩下的,德分,才分均大于等于最低录取线。
  5. else,pass。

代码段

#include <stdlib.h>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
struct student {
    int id;
    short virtueScore;
    short talentScore;
    short totalScore;
};
// 根据总分排序的比较器
bool sortByTotalScoreCompare(student s1, student s2);
// 如果总分相同,根据德分降序排序的比较器
bool sortByVirtueScoreCompare(student s1, student s2);
// 如果总分,德分均相同,根据id升序排序的比较器
bool sortByIdCompare(student s1, student s2);

// 根据总分降序排序
void sortByTotalScore(student studentArray[], int N);
// 总分相同,根据德分降序排序,如果总分相同,德分也相同,根据id升序排序
void sortByVirtueScore(student studentArray[], int N);
// 如果总分相同,德分也相同,根据id升序排序
void sortById(student studentArray[], int start, int end);

// 输出学生信息
void printStudentInfo(student studentArray[], int N);

int main() {
    // 考生总数,录取最低分数线,优先录取线
    int N, L, H;
    // 使用scanf和print替换cin 和 cout 可以解决超时问题,难搞哦	
	// 但是为什么我测试的结果是cout 要比print快呢?、、、
    // cin >> N >> L >> H;
    scanf("%d %d %d", &N, &L, &H);
    int N1 = 0, N2 = 0, N3 = 0, N4 = 0;
    student studentArray1[N];
    student studentArray2[N];
    student studentArray3[N];
    student studentArray4[N];
    int id;
    short virtueScore;
    short talentScore;
    short totalScore;
    // 输入
    for (int i = 0; i < N; i++) {
        student s = student();
        cin >> id >> virtueScore >> talentScore;
        totalScore = virtueScore + talentScore;
        s.id = id;
        s.virtueScore = virtueScore;
        s.talentScore = talentScore;
        s.totalScore = totalScore;
        // I类
        if (virtueScore >= H && talentScore >= H) {
            studentArray1[N1++] = s;
            // II类
        } else if (virtueScore >= H && talentScore >= L && talentScore < H) {
            studentArray2[N2++] = s;
            // III类
        } else if (virtueScore >= L && virtueScore < H 
          && talentScore >= L && talentScore < H && virtueScore >= talentScore) {
            studentArray3[N3++] = s;
            // IV类
        } else if (virtueScore >= L && talentScore >= L) {
            studentArray4[N4++] = s;
        }
    }
    // 排序
    // 总分降序
    sortByTotalScore(studentArray1, N1);
    sortByTotalScore(studentArray2, N2);
    sortByTotalScore(studentArray3, N3);
    sortByTotalScore(studentArray4, N4);
    // 总分相同,德分降序, 总分德分均相同,再按照id升序
    sortByVirtueScore(studentArray1, N1);
    sortByVirtueScore(studentArray2, N2);
    sortByVirtueScore(studentArray3, N3);
    sortByVirtueScore(studentArray4, N4);
    // 输出
    cout << N1 + N2 + N3 + N4 << endl;
    printStudentInfo(studentArray1, N1);
    printStudentInfo(studentArray2, N2);
    printStudentInfo(studentArray3, N3);
    printStudentInfo(studentArray4, N4);
}

bool sortByTotalScoreCompare(student s1, student s2) {
    // return s1.talentScore > s2.totalScore; 怪我粗心大意
    return s1.totalScore > s2.totalScore;
}

bool sortByVirtueScoreCompare(student s1, student s2) {
    return s1.virtueScore > s2.virtueScore;
}

bool sortByIdCompare(student s1, student s2) {
    return s1.id < s2.id;
}

void sortByTotalScore(student studentArray[], int N) {
    sort(studentArray, studentArray + N, sortByTotalScoreCompare);
}

void sortByVirtueScore(student studentArray[], int N) {
    for (int i = 0; i < N - 1; i++) {
        int j = i + 1;
        while (studentArray[i].totalScore == studentArray[j].totalScore && j < N) {
            j++;
        }
        // 有相同的总分,根据德分排序
        if (i + 1 != j) {
            sort(studentArray + i, studentArray + j, sortByVirtueScoreCompare);
            sortById(studentArray, i, j);
            // 连续相同的总分排序后,可跳过,因为外侧for循环还要++,所以减一
            i = j - 1;
        }
    }
}
void sortById(student studentArray[], int start, int end) {
    for (int i = start; i < end - 1; i++) {
        int j = i + 1;
        while (studentArray[i].virtueScore == studentArray[j].virtueScore && j < end) {
            j++;
        }
        // 有相同的总分,根据德分排序
        if (i + 1 != j) {
            sort(studentArray + i, studentArray + j, sortByIdCompare);
            // 连续相同的总分排序后,可跳过,因为外侧for循环还要++,所以减一
            i = j - 1;
        }
    }
}
void printStudentInfo(student studentArray[], int N) {
    for (int i = 0; i < N; i++) {
        student s = studentArray[i];
        // cout << s.id << " " << s.virtueScore << " " << s.talentScore << endl;
        printf("%d %d %d\n", s.id, s.virtueScore, s.talentScore);
    }
}
posted @ 2020-01-20 22:28  Another7  阅读(123)  评论(0编辑  收藏  举报