zoukankan      html  css  js  c++  java
  • PAT B1080 MOOC期终成绩(C++)

    PAT甲级目录 | PAT乙级目录

    题目描述

    B1080 MOOC期终成绩

    解题思路

    可利用 map 将字符串型的学号转换为整型的序号,方便查找。输入全部成绩后,遍历每个学生同时计算最终成绩,然后将成绩合格的人加入结果数组,最后对结果数组进行排序。

    对于将输出的合格的学生,如果某次考试成绩不存在,那只可能是期中考试。如果其他考试有缺考则不可能合格。所以只要将期中考成绩默认为 -1,最后可直接输出无需另外判断。

    易错点

    • 最终成绩要四舍五入

    也许陌生的知识点

    • if(nametoi.find(id) == nametoi.end()){ nametoi[id] = cnt++;}
      • 可利用 map 将字符串类型的 id 转换成整数序号,方便处理
      • 需要的头文件:map
    • sort(S, S + n, cmp);
      • 排序函数,实现 [first, last) 范围内的排序,可以自定义排序策略 cmp 函数
      • 不带 cmp 参数的 sort 函数实现从小到大排序
      • 所需头文件: algorithm
    • vector<int> ans;
      • 实现变长数组,元素类型可任意指定
        • ans.push_back(num[i])往变长数组末尾中添加一个元素
        • ans.pop_back()删除变长数组中最后一个元素
      • 需要的头文件:vector

    代码示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46

    #include <vector>
    #include <string>
    #include <map>
    #大专栏  PAT B1080 MOOC期终成绩(C++)ta-keyword">include <iostream>
    #include <algorithm>
    using namespace std;
    struct {
    string name;
    int gp = 0, gm = -1, gf = 0, g = 0;
    }newstu;
    vector<Student> stu, ans;
    map<string, int> nametoi;
    bool cmp(Student a, Student b){
    if(a.g == b.g) return a.name < b.name;
    else return a.g > b.g;
    }
    int main() {
    int n1, n2, n3;
    cin >> n1 >> n2 >> n3;
    string id;
    int score, i, cnt = 0;
    for(i = 0; i < n1 + n2 + n3; i++){
    cin >> id >> score;
    if(nametoi.find(id) == nametoi.end()){
    newstu.name = id;
    stu.push_back(newstu);
    nametoi[id] = cnt++;
    }
    if(i < n1) stu[nametoi[id]].gp += score;
    else if(i < n1 + n2) stu[nametoi[id]].gm = score;
    else stu[nametoi[id]].gf = score;
    }
    for(int i = 0; i < cnt; i++){
    if(stu[i].gp >= 200){ // 计算最终成绩,并保存合格的学生
    stu[i].g = (stu[i].gm > stu[i].gf) ? (int)((stu[i].gm * 4 + stu[i].gf * 6 + 5)/10) : stu[i].gf;
    if(stu[i].g >= 60) ans.push_back(stu[i]);
    }
    }
    sort(ans.begin(), ans.begin() + ans.size(), cmp);
    for(int i = 0; i < ans.size(); i++){
    cout << ans[i].name;
    printf(" %d %d %d %dn", ans[i].gp, ans[i].gm, ans[i].gf, ans[i].g);
    }
    return 0;
    }
  • 相关阅读:
    创建对象的模式
    linux下安装node v12.16.3
    es6知识点总结
    在阿里云上部署的node服务器不能通过公网IP访问
    angular 1 input中选中状态绑定
    让一个元素水平垂直居中
    语录收集
    跨域
    事件循环
    git的常用命令
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12284876.html
Copyright © 2011-2022 走看看