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

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

输入格式:

输入第一行给出 3 个正整数,分别为:N(≤10),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

  1. 14 60 80
  2. 10000001 64 90
  3. 10000002 90 60
  4. 10000011 85 80
  5. 10000003 85 80
  6. 10000004 80 85
  7. 10000005 82 77
  8. 10000006 83 76
  9. 10000007 90 78
  10. 10000008 75 79
  11. 10000009 59 90
  12. 10000010 88 45
  13. 10000012 80 100
  14. 10000013 90 99
  15. 10000014 66 60

输出样例:

  1. 12
  2. 10000013 90 99
  3. 10000012 80 100
  4. 10000003 85 80
  5. 10000011 85 80
  6. 10000004 80 85
  7. 10000007 90 78
  8. 10000006 83 76
  9. 10000005 82 77
  10. 10000002 90 60
  11. 10000014 66 60
  12. 10000008 75 79
  13. 10000001 64 90

思路

这题就三步走:

  • 读入数据,给各个考生贴标签
  • <algorithm>库里的sort函数帮我们排序
  • 输出结果

代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. struct Student {
  6. int ID; /* 准考证号 */
  7. int DeScore; /* 德分 */
  8. int CaiScore; /* 才分 */
  9. int sum; /* 总分 */
  10. int tier; /* 梯度(第几类考生) */
  11. }stu[100001];
  12. bool cmp(Student a, Student b) { /* 我们自己写sort函数的比较规则 */
  13. if(a.tier != b.tier) {
  14. return a.tier < b.tier; /* 类别小的在前 */
  15. }
  16. else if(a.sum != b.sum) {
  17. return a.sum > b.sum; /* 类别相同时,总分大的在前 */
  18. }
  19. else if(a.DeScore != b.DeScore) {
  20. return a.DeScore > b.DeScore; /* 总分相同时,德分大的在前 */
  21. }
  22. else {
  23. return a.ID < b.ID; /* 德分相同时,准考证号小的在前 */
  24. }
  25. }
  26. int main() {
  27. int number, L, H;
  28. /** 读入学生个数number 最低录取线L 优先录取线H*/
  29. scanf("%d%d%d", &number, &L, &H);
  30. int counter = number; /* counter记录及格人数 */
  31. for(int i = 0; i < number; i++) {
  32. scanf("%d %d %d", &stu[i].ID, &stu[i].DeScore, &stu[i].CaiScore);
  33. stu[i].sum = stu[i].CaiScore + stu[i].DeScore;
  34. if(stu[i].CaiScore < L || stu[i].DeScore < L) {
  35. stu[i].tier = 5; /* 放在最后的五等人 */
  36. counter--; /* 及格人数-1 */
  37. }
  38. else if(stu[i].CaiScore >= H && stu[i].DeScore >= H){
  39. stu[i].tier = 1;
  40. }
  41. else if(stu[i].DeScore >= H && stu[i].CaiScore < H) {
  42. stu[i].tier = 2;
  43. }
  44. else if(stu[i].DeScore < H && stu[i].CaiScore < H && stu[i].DeScore >= stu[i].CaiScore) {
  45. stu[i].tier = 3;
  46. }
  47. else {
  48. stu[i].tier = 4;
  49. }
  50. }
  51. printf("%d\n", counter);
  52. sort(stu, stu + number, cmp); /* 用sort函数自动帮我们排序 */
  53. for(int i = 0; i < counter; i++) {
  54. printf("%d %d %d\n", stu[i].ID, stu[i].DeScore, stu[i].CaiScore);
  55. }
  56. return 0;
  57. }