PAT甲级1137 Final Grading (25分)|C++实现

一、题目描述

原题链接
在这里插入图片描述

Input Specification:

在这里插入图片描述

​​Output Specification:

在这里插入图片描述

Sample Input:

6 6 7
01234 880
a1903 199
ydjh2 200
wehu8 300
dx86w 220
missing 400
ydhfu77 99
wehu8 55
ydjh2 98
dx86w 88
a1903 86
01234 39
ydhfu77 88
a1903 66
01234 58
wehu8 84
ydjh2 82
missing 99
dx86w 81

Sample Output:

missing 400 -1 99 99
ydjh2 200 98 82 88
dx86w 220 88 81 84
wehu8 300 55 84 84

二、解题思路

结构体排序题,还是比较简单的,不过由于题目没有对学生进行数字上的编号,所以我这里建立了一个字符串和结构体的映射unordered_map<string, Student>。然后对编程成绩、期中成绩和期末成绩分别进行输入即可,注意这道题是要求我们进行四舍五入的,所以我们要用到round函数。详情见代码注释。

三、AC代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<cmath>
using namespace std;
struct Student
{
    
    
    string id;
    int progmme = 0;
    double mid = -1, fnl = -1, Grade;
};
unordered_map<string, Student> mp;  //每个ID对应的学生信息
bool cmp(Student a, Student b)  //排序函数,先按成绩从高到低,若成绩相同,按照ID从小到大
{
    
    
    if(round(a.Grade) != round(b.Grade))  return round(a.Grade) > round(b.Grade);
    else return a.id.compare(b.id) < 0;
}
int main()
{
    
    
    int P, M, N, prg;
    double grade;
    string tmp;
    scanf("%d%d%d", &P, &M, &N);
    vector<Student> ans;    //存放结果
    for(int i=0; i<P; i++)  //输入编程成绩
    {
    
    
        cin >> tmp;
        scanf("%d", &prg);
        if(prg >= 200)  //只存200以上的即可
        {
    
    
            mp[tmp].id = tmp;
            mp[tmp].progmme = prg;
        }
    }
    for(int i=0; i<M; i++)      //输入期中成绩
    {
    
    
        cin >> tmp;
        scanf("%lf", &grade);
        if(mp[tmp].progmme != 0)    mp[tmp].mid = grade;
    }
    for(int i=0; i<N; i++)
    {
    
    
        cin >> tmp;
        scanf("%lf", &grade);
        if(mp[tmp].progmme >= 200)    //只更新有效信息即可
        {
    
    
            mp[tmp].fnl = grade;
            if(mp[tmp].mid == -1 || mp[tmp].mid <= mp[tmp].fnl) mp[tmp].Grade = mp[tmp].fnl;
            else    mp[tmp].Grade = 0.4 * mp[tmp].mid + 0.6 * mp[tmp].fnl;
            if(round(mp[tmp].Grade) >= 60.0)   ans.push_back(mp[tmp]);  //存入结果数组
        }
    }
    sort(ans.begin(), ans.end(), cmp);  //排序
    for(int i=0; i<ans.size(); i++)
    {
    
    
        printf("%s %d %.0f %.0f %.0f\n", ans[i].id.c_str(), ans[i].progmme, ans[i].mid, ans[i].fnl, round(ans[i].Grade));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42393947/article/details/109046703
今日推荐