个人练习-PAT甲级-1137 Final Grading

题目链接https://pintia.cn/problem-sets/994805342720868352/problems/994805345401028608

题目大意:给出三张表,分别记录了一堆人的【id和作业成绩】【id和期中成绩】【id和期末成绩】。总评成绩的计算是期中和期末成绩加权求和。要求【作业成绩>=200】且【总评>=60】才能得到证书。求最后得到证书的人的信息的排序。排序先按总评成绩降序,再按id升序。

思路:id是distinct的,直接上map读就完事。读完了遍历,能拿证书的放进vector里。最后排序、输出。但是要注意回车键,记得把回车键用getchar()吃掉再cin读id

   for (int i = 0; i < P; i++) {
        cin >> id >> score;
        getchar();
        sheet[id].p = score;
   }
    for (int i = 0; i < M; i++) {
        cin >> id >> score;
        getchar();
        sheet[id].mid = score;
    }
    for (int i = 0; i < N; i++) {
        cin >> id >> score;
        getchar();
        sheet[id].final = score;
    }

完整代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>


using namespace std;


class Grade{
public:
    int p, mid, final, total;

    Grade(){p = mid = final  = total = -1;}
};


class Node {
public:
    string id;
    int total;

    Node(){}
    Node(string id, int total):id(id), total(total){}
};


bool cmp(Node x, Node y) {
    if (x.total != y.total)
        return x.total > y.total;
    else
        return x.id < y.id;
}


int main() {
   int P, M, N, score;
   string id;
   scanf("%d %d %d\n", &P, &M, &N);
   map<string, Grade> sheet;
   vector<Node> list;
   for (int i = 0; i < P; i++) {
        cin >> id >> score;
        getchar();
        sheet[id].p = score;
   }
    for (int i = 0; i < M; i++) {
        cin >> id >> score;
        getchar();
        sheet[id].mid = score;
    }
    for (int i = 0; i < N; i++) {
        cin >> id >> score;
        getchar();
        sheet[id].final = score;
    }

    for (auto it = sheet.begin(); it != sheet.end(); it++) {
        if (it->second.p >= 200) {
            if (it->second.mid > it->second.final)
                it->second.total = round(.4*it->second.mid + .6*it->second.final);
            else
                it->second.total = it->second.final;
            if (it->second.total >= 60)
                list.push_back(Node(it->first, it->second.total));
        }
    }

    sort(list.begin(), list.end(), cmp);
    for (int i = 0; i < list.size(); i++)
        cout << list[i].id << " " << sheet[list[i].id].p << " " << sheet[list[i].id].mid << " "
        << sheet[list[i].id].final << " " << list[i].total << endl;


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值