Contents
  1. 1. 输入格式:
  2. 2. 输出格式:
  3. 3. 输入样例:
  4. 4. 输出样例:

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

分析:这题目主要是考察结构体的运用和根据自己的需要通过改写cmp进行排序。先定义一个结构体,把输入的参数通过结构体的形式存储起来。在cmp中,函数返回的是bool类型,return A.id < B.id表示根据id升序排列(从小到大);return A.moral_score > B.moral_score;表示根据德分降序排列(从大到小)。在输入的时候就要判断该考生属于哪一类,放到对应的vector中,如果考生德分或者才分没有达到最底线,用count记录下来,n-count就是被录取的考生人数。最需要注意的一点是在输出的时候不能用cout,一定要用printf,否则会超时。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct
{
int id;
int moral_score;
int tal_score;
}Student;
bool cmp(Student A,Student B)
{
if (A.moral_score + A.tal_score != B.moral_score + B.tal_score)
return A.moral_score + A.tal_score > B.moral_score + B.tal_score;
else if (A.moral_score != B.moral_score )
return A.moral_score > B.moral_score;
else return A.id < B.id;
}
int main()
{
int n,low,high,i,j,score1,score2,count=0;
int id;
vector<Student>v[4];
cin >> n >> low >> high;
for (i=0;i<n;i++)
{
cin >> id >> score1 >> score2;
Student s;
s.id = id;
s.moral_score = score1;
s.tal_score = score2;
if (score1 >= high && score2 >= high)
v[0].push_back(s);
else if ((score2>=low && score2<=high) && score1 >= high)
v[1].push_back(s);
else if (score1>=low && score2 >= low && score1 >= score2)
v[2].push_back(s);
else if (score1>=low && score2 >= low)
v[3].push_back(s);
else count++;
}
cout << n-count << endl;
for (i=0;i<4;i++)
{
sort(v[i].begin(),v[i].end(),cmp);
for (j=0;j<v[i].size();j++)
printf("%d %d %d\n",v[i][j].id,v[i][j].moral_score,v[i][j].tal_score);
}
return 0;
}
Contents
  1. 1. 输入格式:
  2. 2. 输出格式:
  3. 3. 输入样例:
  4. 4. 输出样例: