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類,分類之后還要進行排序,而排序的規則又是“嵌套”的那種,可我連個排序都寫不好、、、可以先進行分類,分類到四個陣列中,然后各自進行排序,錄取要保證德分和才分均大于等于最低錄取線,
- I類,德分和才分均大于等于優先錄取線,
- II類,德分大于等于優先錄取線,并且才分大于等于最低錄取線,才分小于優先錄取線,
- III類,德分,才分均大于等于最低錄取線,小于優先錄取線,并且德分大于等于才分,
- IV類,其余剩下的,德分,才分均大于等于最低錄取線,
- 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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/103929.html
標籤:其他
上一篇:ping耗服務器資源么?
