宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”
现给出一批考生的德才分数,请根据司马光的理论给出录取排名。
输入第一行给出 3 个正整数,分别为:N(≤105),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。
随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。
输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。
思路:分为四类 ①“德才兼备” ②“德胜才” ③“德才兼亡”但是符合② ④不满足①②③,但是达到了最低录取线。 使用四个容器对应上面四类,还要注意同分排序的情况。总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。 代码:
#include<iostream> #include<algorithm> #include<vector> using namespace std; /* 所有考生分类四类1、德才兼备(德才分数都大于等于H) 2、德胜才(才分不到H,但是德分大于等于H) 3、才德兼亡但尚有德胜才(德才分均低于H,但是得分大于才分) 4、达到了最低线的其他学生(德才分>L) 先将各类学生内部进行排序,再按1,2,3,4鲜花输出各类学生排名。 */ struct decai { int ID; int defen; int caifen; int total; }; bool compare(decai a, decai b) { if (a.total > b.total) return true; else if (a.total == b.total && a.defen > b.defen)//同分按德分高低排序 return true; else if (a.total == b.total && a.defen == b.defen && a.ID < b.ID)//同分,德分同分按ID递减排序 return true; else return false; } void coutdata(vector<decai> c) { for (int i = 0; i < c.size(); i++) { cout << c[i].ID <<" "<<c[i].defen << " "<< c[i].caifen; cout << endl; } } int main() { int numper, H, L,ID,defen,caifen,count=0; cin >> numper >> L >> H; decai temp; vector<decai>first, second, third, fourth;//对应四种情况 for (int i = 0; i < numper; i++) { scanf("%d %d %d", &ID, &defen, &caifen); if (defen >= L && caifen >= L) count++; temp.defen = defen; temp.caifen = caifen; temp.ID = ID; temp.total=defen+caifen; if (temp.defen >= H && temp.caifen >= H) first.push_back(temp); else if (temp.defen >= H && temp.caifen >= L) second.push_back(temp); else if (temp.defen >= L &&temp.caifen >= L&& temp.defen >= temp.caifen) third.push_back(temp); else if (temp.defen >= L && temp.caifen >= L) fourth.push_back(temp); } sort(first.begin(), first.end(), compare); sort(second.begin(), second.end(), compare); sort(third.begin(), third.end(), compare); sort(fourth.begin(), fourth.end(), compare); cout << count << endl; coutdata(first); coutdata(second); coutdata(third); coutdata(fourth); return 0; }