PAT甲刷题——U4.7

注意:允许转载,但转载请注明作者和出处

最重要:结构体构造函数

最重要参考链接(个人觉得写的很好,但是记不住,。)
参考链接
看了之后有点子一知半解,我也没完全参透。。。抽时间再遇见了同型题我补上

1561. PAT 评测

题目

在这里插入图片描述

解析

我觉得二刷的时候补齐吧,。这题很难

答案

#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
const int K = 6;
int n, k, m;
struct Student
{
    int user_id;
    int grade[K];
    int total, cnt;
    int rank;
    
    Student(){}
    Student(int _uid) : user_id(_uid)
    {
        for (int i = 1; i <= k; i ++) grade[i] = -2;
        total = cnt = 0;
        rank = 0;
    }
    bool has_submitted()
    {
        for (int i = 1; i <= k; i ++)
            if (grade[i] >= 0) return true;
        return false;
    }
    bool operator< (const Student& t) const
    {
        if (total != t.total) return total > t.total;
        if (cnt != t.cnt) return cnt > t.cnt;
        return user_id < t.user_id;
    }
};
unordered_map<int, Student> students;
vector<Student> res;
int all_grade[K];
int main()
{
    scanf("%d %d %d", &n, &k, &m);
    for (int i = 1; i <= k; i ++) scanf("%d", &all_grade[i]);;
    
    while (m --)
    {
        int user_id, t_id, grade;
        scanf("%d %d %d", &user_id, &t_id, &grade);
        // 因为一个user_id可能会反复提交,故t_id对应成绩应取最大值
        if (!students.count(user_id))
            students[user_id] = Student(user_id);
        students[user_id].grade[t_id] = max(students[user_id].grade[t_id], grade);
    }
    // 在提交的情况下,找到user_id对应的总分和题目取得满分的数量
    for (auto &s : students)
    {
        auto &ss = s.second;
        if (ss.has_submitted())
        {
            for (int i = 1; i <= k; i ++)
            {
                ss.total += max(0, ss.grade[i]);
                if(ss.grade[i] == all_grade[i]) ss.cnt ++;
            }
            res.push_back(ss);
        }
    }

    sort(res.begin(), res.end());
    // for (auto r : res) cout << r.user_id << ' ' << r.total << endl;
    // 下面语句必须是i < res.size(),为什么不能是i < n呢
    // 因为n是user总数,但是并不是所有的user都提交题了,
    // 有一些人完全没提交,那就不统计这部分人
    for (int i = 0; i < res.size(); i ++)
    {
        if (!i || res[i].total != res[i - 1].total)
            res[i].rank = i + 1;
        else res[i].rank = res[i - 1].rank;
    }
    for (auto r : res)
    {
        printf("%d %05d %d", r.rank, r.user_id, r.total);
        for (int i = 1; i <= k; i ++)
        {
            if (r.grade[i] == -2) printf(" -");
            else if (r.grade[i] == -1) printf(" 0");
            else printf(" %d", r.grade[i]);
        }
        puts("");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值