1085 PAT单位排行 (25 分)
每次 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
=================================
输入3项:考生号、成绩、校名(大小写混合)。
输出4项: 学校排名、校名(全小写)、加权总分、人数。
可以看出,考生号用不到,不用保存。
校名转换为全小写。用unordered_map<string, int> cnt;存储校名、人数。
用unordered_map<string, double> sco; 存储校名、成绩。
输入部分结束,接下来是处理。
解释一下这俩行代码:
for(auto it = cnt.begin(); it != cnt.end(); it++)
ans.push_back(node{it->first, (int)sum[it->first], cnt[it->first]}); //校名,加权总分、人数存到vector<node>里。
存到vector<node> ans;之后,按题目规则排序。
这题的输出是一个小模板,就在PAT中也有几题需要按这种形式输出。(即如果xx和xx相同,则他俩排名并列)
建议理解并记住。
//本题要求与前一学校加权总分相同时,排名并列。
int rank = 0, pres = -1; //rank是排名,pres是前一学校的加权总分(初始为-1,因为遍历第一个学校之前没有学校)。
for(int i = 0; i < ans.size(); i++){
if(pres != ans[i].tws) rank = i+1;
pres = ans[i].tws;
printf("%d %s %d %d\n", rank, ans[i].school.c_str(), ans[i].tws, ans[i].pNum);
}
反思:(可跳过不看):
map是尖括号,竟然写错...
tolower(),toupper()等等再在头文件cc1type中。
==================================
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <cctype>
using namespace std;
struct node{
string school;
int tws, pNum;
};
bool cmp(node a, node b){
if(a.tws != b.tws) return a.tws > b.tws;
else if(a.pNum != b.pNum) return a.pNum < b.pNum;
else return a.school < b.school;
}
int main(){
int n;
string id, school;
double sco;
cin >> n;
unordered_map<string, int> cnt;
unordered_map<string, double> sum;
for(int i = 0; i < n; i++){
cin >> id >> sco >> school;
for(int j = 0; j < school.size(); j++)
school[j] = tolower(school[j]);
if(id[0] == 'B') sco /= 1.5;
else if(id[0] == 'T') sco *= 1.5;
sum[school] += sco;
cnt[school]++;
}
vector<node> ans;
for(auto it = cnt.begin(); it != cnt.end(); it++)
ans.push_back(node{it->first, (int)sum[it->first], cnt[it->first]});
sort(ans.begin(), ans.end(), cmp);
printf("%d\n", ans.size());
int rank = 0, pres = -1;
for(int i = 0; i < ans.size(); i++){
if(pres != ans[i].tws) rank = i+1;
pres = ans[i].tws;
printf("%d %s %d %d\n", rank, ans[i].school.c_str(), ans[i].tws, ans[i].pNum);
}
return 0;
}