宋代史學家司馬光在《資治通鑑》中有一段著名的「德才論」:「是故才德全盡謂之聖人,才德兼亡謂之愚人,德勝才謂之君子,才勝德謂之小人。凡取人之術,苟不得聖人,君子而與之,與其得小人,不若得愚人。」ios
現給出一批考生的德才分數,請根據司馬光的理論給出錄取排名。數組
輸入第一行給出 3 個正整數,分別爲:N(≤105),即考生總數;L(≥60),爲錄取最低分數線,即德分和才分均不低於 L 的考生纔有資格被考慮錄取;H(<100),爲優先錄取線——德分和才分均不低於此線的被定義爲「才德全盡」,此類考生按德才總分從高到低排序;才分不到但德分到線的一類考生屬於「德勝才」,也按總分排序,但排在第一類考生以後;德才分均低於 H,可是德分不低於才分的考生屬於「才德兼亡」但尚有「德勝才」者,按總分排序,但排在第二類考生以後;其餘達到最低線 L 的考生也按總分排序,但排在第三類考生以後。測試
隨後 N 行,每行給出一位考生的信息,包括:准考證號 德分 才分
,其中准考證號
爲 8 位整數,德才分爲區間 [0, 100] 內的整數。數字間以空格分隔。spa
輸出第一行首先給出達到最低分數線的考生人數 M,隨後 M 行,每行按照輸入格式輸出一位考生的信息,考生按輸入中說明的規則從高到低排序。當某類考生中有多人總分相同時,按其德分降序排列;若德分也並列,則按准考證號的升序輸出。code
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類。分類以後還要進行排序,而排序的規則又是「嵌套」的那種。可我連個排序都寫很差、、、能夠先進行分類,分類到四個數組中,而後各自進行排序。錄取要保證德分和才分均大於等於最低錄取線。ci
#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); } }