PAT B1080 MOOC期终成绩(C++)

主宰稳场 提交于 2020-02-09 10:39:29
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

代码示例:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
#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;}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!