每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。
输入第一行给出一个正整数 N(≤105),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息:
准考证号 得分 学校
其中准考证号
是由 6 个字符组成的字符串,其首字母表示考试的级别:B
代表乙级,A
代表甲级,T
代表顶级;得分
是 [0, 100] 区间内的整数;学校
是由不超过 6 个英文字母组成的单位码(大小写无关)。注意:题目保证每个考生的准考证号是不同的。
首先在一行中输出单位个数。随后按以下格式非降序输出单位的排行榜:
排名 学校 加权总分 考生人数
其中排名
是该单位的排名(从 1 开始);学校
是全部按小写字母输出的单位码;加权总分
定义为乙级总分/1.5 + 甲级总分 + 顶级总分*1.5
的整数部分;考生人数
是该属于单位的考生的总人数。
学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序输出。
10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu
5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2
存储数据
typedef struct Student {
string id; // 准考证号
double score; // 得分
string school; // 学校
};
typedef struct School {
int ranking; // 排名
string schoolName; // 学校
double schoolScore; // 加权总分
int studentCnt; // 考生人数
};
输出样例中,学校名都是小写的,所以接收输入的时候,也要把学校名转成小写再存起来
Q:怎么记录学校中有多少个考生,总分是多少呢?
A:把它们存进unordered_map<string, School> m
里面,key是学校名,value是学校结构体
Q:存进unordered_map
之后,怎么排序?
A:遍历unorder_map
,由于它的value是一个School结构体,所以把value存进vector<School>
的数组里,再进行排序
Q:并列排名要怎么输出成1 1 3 4 4
的情况?
A:School记录着学校排名,先给第一个ranking赋值为1,先遍历vector<Schhol>
数组,i 从 0 到 学校的数量N,如果当前分数和上一个相同,那么排名就和上一个相同。否则,排名就是 i + 1
还需要注意的是,分数是double类型,输出的时候只输出整数部分
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef struct Student {
string id; // 准考证号
double score; // 得分
string school; // 学校
};
typedef struct School {
int ranking; // 排名
string schoolName; // 学校
double schoolScore; // 加权总分
int studentCnt; // 考生人数
};
// 转小写
void toLowerCase(string &str) {
int len = str.size();
for (int i = 0; i < len; ++i) {
if (str[i] >= ‘A‘ && str[i] <= ‘Z‘) {
str[i] += 32;
}
}
}
// 排序
bool cmp(School& a, School& b) {
if (a.schoolScore != b.schoolScore) {
return a.schoolScore > b.schoolScore;
}
else if (a.studentCnt != b.studentCnt) {
return a.studentCnt < b.studentCnt;
}
else {
return a.schoolName < b.schoolName;
}
}
int main() {
int N;
cin >> N;
vector<Student> stu(N);
vector<School> schools;
unordered_map<string, School> m;
for (int i = 0; i < N; ++i) {
cin >> stu[i].id >> stu[i].score >> stu[i].school;
toLowerCase(stu[i].school);
m[stu[i].school].studentCnt++;
m[stu[i].school].schoolName = stu[i].school;
char c = stu[i].id[0];
if (c == ‘T‘) {
m[stu[i].school].schoolScore += stu[i].score * 1.5;
}
else if (c == ‘A‘) {
m[stu[i].school].schoolScore += stu[i].score;
}
else {
m[stu[i].school].schoolScore += stu[i].score / 1.5;
}
}
// 把value放进数组
for (auto& i : m) {
i.second.schoolScore = (int)i.second.schoolScore;
schools.push_back({ 0,i.second.schoolName,i.second.schoolScore,i.second.studentCnt });
}
sort(schools.begin(), schools.end(), cmp);
cout << schools.size() << endl;
for (int i = 0; i < schools.size(); ++i) {
if (i != 0 && schools[i].schoolScore == schools[i - 1].schoolScore) {
schools[i].ranking = schools[i - 1].ranking;
}
else {
schools[i].ranking = i + 1;
}
printf("%d %s %.0lf %d\n",
schools[i].ranking,
schools[i].schoolName.c_str(),
schools[i].schoolScore,
schools[i].studentCnt);
}
return 0;
}
// TODO
原文:https://www.cnblogs.com/47Pineapple/p/12953832.html