PAT甲级1137Final Grading

题目链接

https://pintia.cn/problem-sets/994805342720868352/problems/994805345401028608

题解

题目要求

  • 要获得证书,一个学生的在线编程作业至少要拿到200分,最后评分G至少要拿到60分。

    如果期中分数大于期末分数,则G等于期中成绩乘以40%+期末成绩乘以60%,否则G等于期末成绩

  • 输入

    • 第一行

      三个数字,都不超过10000

      • P:完成网上在线作业的学生的数量
      • M:参加期中考试的学生的数量
      • N:参加期末考试的学生的数量
    • P个学生网上在线作业的成绩

    • M个学生期中考试的成绩

    • N个学生期末考试的成绩

  • 输出

    输出获得证书的学生的ID及其3个成绩,按G(四舍五入到整数)降序输出,如果G相等,则按ID升序输出。如果某学生某些成绩不存在,则输出-1

英语

  • assignment

    工作、任务

    分配、指派

  • certificate

    证明

  • round

    四舍五入

思路

  1. 先读取在线编程作业分数,如果小于200,就不记录该学生的信息
  2. 在第一步记录的学生范围内,记录其期中分数
  3. 在第一步记录的学生范围内,记录其期末分数,同时计算其最后分数G,如果G大于60,则存入vector
  4. 将vector排序
  5. 输出

代码

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Problem: PAT Advanced 1137
// URL: https://pintia.cn/problem-sets/994805342720868352/problems/994805345401028608
// Tags: unordered_map vector sort

#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

struct Student{
string id;
int gp=-1;
int gm=-1;
int gf=-1;
int g=-1;
};

bool studentCmp(Student& s1, Student& s2){
return s1.g == s2.g ? s1.id < s2.id : s1.g > s2.g;
}

int main()
{
int p, m, n;
unordered_map<string, Student> students_map;
cin >> p >> m >> n;

string str;
int score;
for (int i = 0; i < p; i++){
cin >> str >> score;
if (score >= 200){ // 在线编程分数大于200才行(不允许该分数不存在)
students_map[str].id = str;
students_map[str].gp = score;
}
}
for (int i = 0; i < m; i++){
cin >> str >> score;
if (students_map.find(str) != students_map.end()) // 避免创建新的学生
students_map[str].gm = score;
}
vector<Student> students_vec;
for (int i = 0; i < n; i++){
cin >> str >> score;
if (students_map.find(str) != students_map.end()){ // 避免创建新的学生
students_map[str].gf = score;
if (students_map[str].gm > students_map[str].gf)
students_map[str].g = round(students_map[str].gm * 0.4 + students_map[str].gf * 0.6);
else
students_map[str].g = students_map[str].gf;
if (students_map[str].g >= 60)
students_vec.push_back(students_map[str]);
}
}

sort(students_vec.begin(), students_vec.end(), studentCmp);

for (auto it = students_vec.begin(); it != students_vec.end(); it++)
printf("%s %d %d %d %d\n", it->id.c_str(), it->gp, it->gm, it->gf, it->g);
return 0;
}

参考链接

https://blog.csdn.net/weixin_44385565/article/details/88669948

https://blog.csdn.net/liuchuo/article/details/79064895


作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!