0%

PAT-1012 The Best Rank

1012 The Best Rank

题意

给定一些列学生在给定科目的得分情况,计算每个学生排名最好的科目,或者平均分排名最好

思路

题目要求给出每个学生A,C,M,E的分数,计算每个学生名次最好的科目,且4个科目的重要程度为A>C>M>E
求解过程是将每科的分数单独排序,计算出每个科目分数所在的名次,并用map存下该分数的名次,然后可以直接通过学生的分数得到所在名次

对于A平均分排序,因为浮点数不适合做key值,所以我采用总分排名

注意:相同分数的名次相同,其后的名次则为累计名次

源码

line_number: true
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct STUDENT_GRADE{
int C;
int M;
int E;
int A;
};

int cmp1(int a,int b)
{
return b<a;
}

char szTest[4] = {'C', 'M', 'E', 'A'};
int main()
{
int iN, iM;
cin >>iN >>iM;
vector<int> vecGrade[4]; //记录每种科目的所有分数,并排序
map<int, int> mapGradeIndex[4]; //记录分数在该科目中的位次
unordered_map<string, STUDENT_GRADE> mapStudentGrade; //记录学生的分数
string strStudentId;
for(int i = 0; i < iN; i++)
{

int iC, iM, iE;
cin >> strStudentId >> iC >> iM >> iE;
STUDENT_GRADE stTudentGrade;
stTudentGrade.C = iC;
stTudentGrade.M = iM;
stTudentGrade.E = iE;
stTudentGrade.A = iC + iM + iE;
mapStudentGrade[strStudentId] = stTudentGrade;
vecGrade[0].push_back(iC);
vecGrade[1].push_back(iM);
vecGrade[2].push_back(iE);
vecGrade[3].push_back(stTudentGrade.A);
}

//对每个科目的分数进行排序,并计算出位次
for(int i = 0; i < 4; i++)
{
sort(vecGrade[i].begin(), vecGrade[i].end(), cmp1); //从大到小倒叙排列
int iNextRank = 1; //下一个名次的位次
int iLastNum = 301; //总分最大为300,所以这里取301
//计算位次
for(int j = 0; j < vecGrade[i].size(); j++)
{
if(iLastNum > vecGrade[i][j]) //上一名的分数大于本轮次分数,名次顺延续
{
mapGradeIndex[i][vecGrade[i][j]] = iNextRank;
}
else
{
mapGradeIndex[i][vecGrade[i][j]] = mapGradeIndex[i][vecGrade[i][j]]; //和上一名分数相同,则名次也相同
}
iLastNum = vecGrade[i][j]; //保留本轮分数
iNextRank ++; // 记录分数位次
}
}

for(int i = 0; i < iM; i++)
{

cin >> strStudentId;
if(mapStudentGrade.find(strStudentId) == mapStudentGrade.end())
{
cout<<"N/A"<<endl;
continue;
}
int iTest = 3;
int iRank = mapGradeIndex[3][mapStudentGrade[strStudentId].A];

if(iRank > mapGradeIndex[0][mapStudentGrade[strStudentId].C])
{
iRank = mapGradeIndex[0][mapStudentGrade[strStudentId].C];
iTest = 0;
}

if(iRank > mapGradeIndex[1][mapStudentGrade[strStudentId].M])
{
iRank = mapGradeIndex[1][mapStudentGrade[strStudentId].M];
iTest = 1;

}
if(iRank > mapGradeIndex[2][mapStudentGrade[strStudentId].E])
{
iRank = mapGradeIndex[2][mapStudentGrade[strStudentId].E];
iTest = 2;

}
cout<< iRank << " "<< szTest[iTest] <<endl;
}
return 0;
}