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:

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:

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:

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

Sample Output:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2
  • 题意:给出每个学生的id、分数、学校,学校名称不区分大小写,输出学校排名、学校名称、总加权成绩、学校参赛人数。学校名称输出时候以小写方式输出。
  • 分析:学校名称不区分大小写,所以用一个unordered_map<string, int>给每个学校编号,编号从0开始,逐行输入数据后,先把学校名城转为小写,再判断该学校是否出现过,没出现过就给这个学校编一个号,同时要记录下出现过多少个不同的学校,然后就是把分数加到这个学校对应的结构体中,输入结束后,给学校根据要求编写cmp函数做排名,这里值得注意的是 排名实际上比较的是各学校加权总分四舍五入之后的总分 ,因此比较时通过int做了强制类型转换。最后输出结果就好了。
#include <bits/stdc++.h>
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;
}
发布了27 篇原创文章 · 获赞 0 · 访问量 103

猜你喜欢

转载自blog.csdn.net/charjindev/article/details/104311680