混沌者 pat basic 练习八十 MOOC期终成绩

心得:

unordered_map因为不用排序,所以存储比map快,如果不需要用map的自动排序而着重于速度,使用unordered_map

题目:

对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分,然后总评获得不少于60分(满分100)。总评成绩的计算公式为 G=(Gmid−term×40%+Gfinal×60%)G = (G_{mid-term}\times 40\% + G_{final}\times 60\%)G=(G​mid−term​​×40%+G​final​​×60%),如果 Gmid−term>GfinalG_{mid-term} > G_{final}G​mid−term​​>G​final​​;否则总评 GGG 就是 GfinalG_{final}G​final​​。这里 Gmid−termG_{mid-term}G​mid−term​​ 和 GfinalG_{final}G​final​​ 分别为学生的期中和期末成绩。

现在的问题是,每次考试都产生一张独立的成绩单。本题就请你编写程序,把不同的成绩单合为一张。

输入格式:

输入在第一行给出3个整数,分别是 P(做了在线编程作业的学生数)、M(参加了期中考试的学生数)、N(参加了期末考试的学生数)。每个数都不超过10000。

接下来有三块输入。第一块包含 P 个在线编程成绩 GpG_pG​p​​;第二块包含 M 个期中考试成绩 Gmid−termG_{mid-term}G​mid−term​​;第三块包含 N 个期末考试成绩 GfinalG_{final}G​final​​。每个成绩占一行,格式为:学生学号 分数。其中学生学号为不超过20个字符的英文字母和数字;分数是非负整数(编程总分最高为900分,期中和期末的最高分为100分)。

输出格式:

打印出获得合格证书的学生名单。每个学生占一行,格式为:

学生学号 GpG_pG​p​​Gmid−termG_{mid-term}G​mid−term​​GfinalG_{final}G​final​​GGG

如果有的成绩不存在(例如某人没参加期中考试),则在相应的位置输出“−1-1−1”。输出顺序为按照总评分数(四舍五入精确到整数)递减。若有并列,则按学号递增。题目保证学号没有重复,且至少存在1个合格的学生。

 

 

思路:

我使用map容器

声明一个结构体,包含学生学号,编程分,期中分,期末分,总分

然后学生学号作为key,结构体作为value存进map中

然后进行筛选删除

最后根据value排序

 

代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
struct node
{
    string id="";
    int online_score=-1;
    int mid_score=-1;
    int final_score=-1;
    int sum=-1;
};
bool cmp(pair<string,node> a, pair<string,node> b)
{
    if(a.second.sum!=b.second.sum)
    {
        return a.second.sum>b.second.sum;
    }
    return a.second.id<b.second.id;
}
int main()
{
    ios::sync_with_stdio(false);
    int online_num,mid_num,final_num;
    cin>>online_num>>mid_num>>final_num;
    map<string,node> m;
    for(int i=0;i<online_num;i++)
    {
        node temp;
        cin>>temp.id>>temp.online_score;
        if(temp.online_score>=200)
            m[temp.id]=temp;
    }
    for(int i=0;i<mid_num;i++)
    {
        string s_temp;
        int i_temp;
        cin>>s_temp>>i_temp;
        if(m.find(s_temp)!=m.end())
        {
            m[s_temp].mid_score=i_temp;
        }
    }
    for(int i=0;i<final_num;i++)
    {
        string s_temp;
        int i_temp;
        cin>>s_temp>>i_temp;
        if(m.find(s_temp)!=m.end())
        {
            m[s_temp].final_score=i_temp;
            if(m[s_temp].final_score<m[s_temp].mid_score)
            {
                m[s_temp].sum=round(0.4*m[s_temp].mid_score+0.6*m[s_temp].final_score);
            }
            else
            {
                m[s_temp].sum=m[s_temp].final_score;
            }
        }
    }
    for(auto it=m.begin();it!=m.end();)
    {
        if(it->second.sum<60)
            it=m.erase(it);
        else
            it++;
    }
    vector<pair<string,node>> res(m.begin(),m.end());
    sort(res.begin(),res.end(),cmp);
    for(int i=0;i<res.size();i++)
    {
        cout<<res.at(i).second.id<<" "<<res.at(i).second.online_score<<" "<<res.at(i).second.mid_score<<" "<<res.at(i).second.final_score<<" "<<res.at(i).second.sum<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值