PAT B1085 PAT单位排行 (25分)
条评论PAT甲级:B1085 PAT单位排行 (25 分)
After each PAT, the PAT Center will announce the ranking of institutions based on their students’ performances. Now you are asked to generate the ranklist.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤105), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:
1 | ID Score School |
where ID
is a string of 6 characters with the first one representing the test level: B
stands for the basic level, A
the advanced level and T
the top level; Score
is an integer in [0, 100]; and School
is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that ID
is unique for each testee.
Output Specification:
For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:
1 | Rank School TWS Ns |
where Rank
is the rank (start from 1) of the institution; School
is the institution code (all in lower case); ; TWS
is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5
, where ScoreX
is the total score of the testees belong to this institution on level X
; and Ns
is the total number of testees who belong to this institution.
The institutions are ranked according to their TWS
. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns
. If there is still a tie, they shall be printed in alphabetical order of their codes.
Sample Input:
1 | 10 |
Sample Output:
1 | 5 |
- 题意:给出每个学生的id、分数、学校,学校名称不区分大小写,输出学校排名、学校名称、总加权成绩、学校参赛人数。学校名称输出时候以小写方式输出。
- 分析:学校名称不区分大小写,所以用一个
unordered_map<string, int>
给每个学校编号,编号从0开始,逐行输入数据后,先把学校名城转为小写,再判断该学校是否出现过,没出现过就给这个学校编一个号,同时要记录下出现过多少个不同的学校,然后就是把分数加到这个学校对应的结构体中,输入结束后,给学校根据要求编写cmp
函数做排名,这里值得注意的是 排名实际上比较的是各学校加权总分四舍五入之后的总分 ,因此比较时通过int
做了强制类型转换。最后输出结果就好了。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
using namespace std;
struct node {
string school;
int cntMember = 0;
double score = 0.0;
} s[100010];
bool cmp(node a, node b) {
if ((int)a.score != (int)b.score) return (int)a.score > (int)b.score;
else if (a.cntMember != b.cntMember) return a.cntMember < b.cntMember;
return a.school < b.school;
}
void toLower(string &s) {
for (int i = 0; i < s.length(); i++)
if (isupper(s[i])) s[i] = s[i] + 32;
}
int main() {
int n, score, cnt = 1;
string id, school;
unordered_map<string, int> mp;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cin >> id >> score >> school;
toLower(school);
int sid = mp[school];
if (sid == 0) {
mp[school] = cnt++;
sid = mp[school];
s[sid].school = school;
}
s[sid].cntMember++;
if (id[0] == 'A') s[sid].score += score;
else if (id[0] == 'B') s[sid].score += score / 1.5;
else s[sid].score += score * 1.5;
}
sort(s + 1, s + cnt, cmp);
printf("%d\n", cnt - 1);
int tscore = (int)s[1].score, r = 1;
for (int i = 1; i < cnt; i++) {
if ((int)s[i].score != tscore) {
r = i;
tscore = s[i].score;
}
printf("%d %s %d %d\n", r, s[i].school.c_str(), (int)s[i].score, s[i].cntMember);
}
return 0;
}
- 本文链接:https://blog.charjin.top/2019/07/10/pat-B1085/
- 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!
分享