1085 PAT单位排行

/**
 * 1.解题思路:两个map,一个cnt用来存储某学校名称对应的参赛人数
 *            另一个sum计算某学校名称对应的总加权成绩。
 *            每次学校名称string school都要转化为全小写
 *            将map中所有学校都保存在vector ans中
 *            类型为node,node中包括学校姓名、加权总分、参赛人数。对ans数组排序
 *            根据题目要求写好cmp函数,最后按要求输出。对于排名的处理:
 *            设立pres表示前一个学校的加权总分,如果pres和当前学校的加权总分不同
 *            说明rank等于数组下标+1,否则rank不变
 * 
 * 2.注意:总加权分数取整数部分是要对最后的总和取整数部分,不能每次都直接用int存储,不然会有一个3分测试点不通过
 *        更新后的pat系统会导致之前使用map的代码最后一个测试点超时,更改为unordered_map即可AC
 * 
 * 3.参考博客:https://www.liuchuo.net/archives/4648
 **/
#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>
#include <unordered_map>
using namespace std;
//学校姓名、加权总分、参赛人数
struct node {
    string school;
    int tws, ns;
};
//首先按加权总分排行
//如有并列,则应对应相同的排名,并按考生人数升序输出
//如果仍然并列,则按单位码的字典序输出。
bool cmp(node a, node b) {
    if (a.tws != b.tws)
        return a.tws > b.tws;
    else if (a.ns != b.ns)
        return a.ns < b.ns;
    else
        return a.school < b.school;
}
int main() {
    int n;
    scanf("%d", &n);
    //存储某学校名称对应的参赛人数
    //unordered_map元素在内部不以任何特定顺序排序,而是组织进桶中。元素放进哪个桶完全依赖于其键的哈希
    unordered_map<string, int> cnt;
    //计算某学校名称对应的总加权成绩
    unordered_map<string, double> sum;
    for (int i = 0; i < n; i++) {
        string id, school;
        cin >> id;
        double score;
        scanf("%lf", &score);
        cin >> school;
        //每次学校名称string school都要转化为全小写
        for (int j = 0; j < school.length(); j++)
            school[j] = tolower(school[j]);
        if (id[0] == 'B')
            score = score / 1.5;
        else if (id[0] == 'T')
            score = score * 1.5;
        sum[school] += score;
        cnt[school]++;
    }
    //将map中所有学校都保存在vector ans中
    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]});
    //对ans数组排序
    sort(ans.begin(), ans.end(), cmp);
    //设立pres表示前一个学校的加权总分
    int rank = 0, pres = -1;
    printf("%d\n", (int)ans.size());
    for (int i = 0; i < ans.size(); i++) {
        //如果pres和当前学校的加权总分不同
        //说明rank等于数组下标+1
        if (pres != ans[i].tws) rank = i + 1;
        pres = ans[i].tws;
        printf("%d ", rank);
        cout << ans[i].school;
        printf(" %d %d\n", ans[i].tws, ans[i].ns);
    }
    return 0;
}
### 关于PAT单位排行C语言测试点2的解析 #### 输入规格说明 对于每个测试案例,在一行中给出总考生数量。随后按照指定格式打印最终排名列表[^3]。 #### 准考证号结构分析 准考证号由6个字符构成,首位字母指示考试等级:`B`表示乙级,`A`表示甲级,而`T`则对应顶级。每位考生的成绩位于区间\[0, 100\]之间,并且学校通过最多含6个小写字母(忽略大小写差异)来编码识别[^4]。 #### 数据处理逻辑实现 为了完成这一任务,可以采用如下方法: - **读取输入数据**:接收并解析来自标准输入的数据流; - **构建字典存储成绩表**:创建一个字典用于保存各学校的累计分数以及参与人数; - **遍历所有记录更新统计信息**:逐条处理每一条参赛者的信息,依据所属学校累积相应分值; - **计算平均分与排序**:基于上述统计数据计算各个机构的加权评分,并据此排列名次; - **输出结果**:依照规定模板展示最终排行榜单。 以下是Python代码片段演示如何执行这些操作: ```python from collections import defaultdict def process_input(): n = int(input()) schools = defaultdict(lambda : {'total_score': 0, 'count': 0}) for _ in range(n): id_, score_str, school_code = input().split() score = int(score_str) # Update statistics for this school schools[school_code.lower()]['total_score'] += score schools[school_code.lower()]['count'] += 1 return dict(schools) def calculate_weighted_scores(school_data): weighted_scores = [] for code, stats in school_data.items(): avg_score = round(stats['total_score']/stats['count']) entry = { 'code': code, 'avg_score': avg_score, 'num_students': stats['count'] } weighted_scores.append(entry) return sorted(weighted_scores, key=lambda x:(-x['avg_score'], x['code'])) if __name__ == "__main__": data = process_input() results = calculate_weighted_scores(data) print(len(results)) for res in results: print(f"{res['code']} {res['num_students']} {res['avg_score']}") ``` 该程序首先收集所有的输入数据并将它们分类整理到不同学校下,接着计算出每个学校的平均得分及其参加的学生数目,最后按要求输出排序后的表格。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值