PAT(乙级)1015

1015. 德才论 (25)

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

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

输入格式:

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

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

输出格式:

输出第1行首先给出达到最低分数线的考生人数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



分析:这个题目,本人实在是想不出什么好的办法,只能暴力解决。直接将它们分成四类,然后去排序。否则排序就很头疼。

这个暴力解决法,在使用cout的时候,还会超时。(我的心里一万只草泥马在奔腾),只好换成printf。估计cin换成scanf,时间还能继续缩短。


代码如下

#include<iostream>
#include<algorithm>
#include<vector>

using std::cin;
using std::cout;
using std::vector;
using std::endl;

typedef struct student
{
  int score1;
  int score2;
  int number;
  int sum;
}STUDENT;
typedef struct student* STU;

bool cmp(STUDENT a, STUDENT b)
{
  if (a.sum != b.sum)
  {
    return a.sum > b.sum;
  }
  else
  {
    if (a.score1 != b.score1)
    {
      return a.score1 > b.score1;
    }
    else
    {
      return a.number < b.number;
    }
  }
}
int main()
{
  //四类学生,因为这个排序要求过于复杂,这样处理比较简单。
  vector<STUDENT> type1;
  vector<STUDENT> type2;
  vector<STUDENT> type3;
  vector<STUDENT> type4;

  STUDENT t;

  int n, l, h, count = 0;
  cin >> n >> l >> h;    //输入人数,分数线,优秀分数线
  int temp[3];
  for (int i = 0; i != n; i++)
  {
    cin >> temp[0] >> temp[1] >> temp[2];
    if (temp[1] >= l && temp[2] >= l)  //德才分均过线
    {
      t.number = temp[0];
      t.score1 = temp[1];
      t.score2 = temp[2];
      t.sum = temp[1] + temp[2];
      if (temp[1] >= h && temp[2] >= h)  //德才兼备
      {
        type1.push_back(t);    //添加
      }
      else if (temp[1] >= h && temp[2] < h)    //德存才亡
      {
        type2.push_back(t);    //添加
      }
      else if (temp[1] < h && temp[2] < h && temp[1] >= temp[2])  //德才皆无
      {
        type3.push_back(t);    //添加  
      }
      else
      {
        type4.push_back(t);    //添加
      }
      count++;
    }
  }
  //按照降序排列
  sort(type1.begin(), type1.end(), cmp);
  sort(type2.begin(), type2.end(), cmp);
  sort(type3.begin(), type3.end(), cmp);
  sort(type4.begin(), type4.end(), cmp);

  cout << count << endl;
  vector<STUDENT>::iterator it;
  for (it = type1.begin(); it != type1.end(); it++)
    printf("%d %d %d
",it->number,it->score1,it->score2);
  for (it = type2.begin(); it != type2.end(); it++)
    printf("%d %d %d
",it->number,it->score1,it->score2);
  for (it = type3.begin(); it != type3.end(); it++)
    printf("%d %d %d
",it->number,it->score1,it->score2);
  for (it = type4.begin(); it != type4.end(); it++)
    printf("%d %d %d
",it->number,it->score1,it->score2);
  return 0;
}

原文地址:https://www.cnblogs.com/zy666/p/10504348.html