想着一个学号对应一个学生信息,于是就用了map来进行数据的输入,然后新建一个map来存储获得证书的学生的信息,由于map无法进行排序,所以把获取整数的数据又转换为vector进行sort排序之后进行输出
注意
- 题中所说的如果总评分数相同则按照学号递增进行排序
- round进行四舍五入
- 学生期中成绩有0分的可能
C++
1 #include <iostream> 2 #include <string> 3 #include <map> 4 #include <cmath> 5 #include <vector> 6 #include <algorithm> 7 8 using namespace std; 9 struct grade { 10 int mid, final, online, sum_grade; 11 } tmp_grade; 12 13 //sort的比较函数,总评高的在前,总评相同按学号字典序排名 14 bool cmp(pair<string, grade> a, pair<string, grade> b) { 15 if(a.second.sum_grade == b.second.sum_grade) return a.first < b.first; 16 return a.second.sum_grade > b.second.sum_grade; 17 } 18 19 int main() { 20 int P = 0, M = 0, N = 0; 21 map<string, grade> students_grade; 22 map<string, grade> Certificate; 23 cin >> P >> M >> N; 24 tmp_grade = {-1, -1, -1, -1}; 25 for (int i = 0; i < P; ++i) { 26 string id; 27 cin >> id >> tmp_grade.online; 28 students_grade.insert(pair<string, grade>(id, tmp_grade)); 29 tmp_grade.online = -1; //每轮清除输入,方便下一次输入 30 } 31 32 for (int i = 0; i < M; ++i) { 33 string id; 34 cin >> id >> tmp_grade.mid; 35 if (students_grade.find(id) == students_grade.end()) students_grade.insert(pair<string, grade>(id, tmp_grade)); 36 else students_grade[id].mid = tmp_grade.mid; //如果学号存在,直接输入,不存在则在map中插入 37 tmp_grade.mid = -1; 38 } 39 40 for (int i = 0; i < N; ++i) { 41 string id; 42 cin >> id >> tmp_grade.final; 43 if (students_grade.find(id) == students_grade.end()) students_grade.insert(pair<string, grade>(id, tmp_grade)); 44 else students_grade[id].final = tmp_grade.final; 45 46 if (students_grade[id].mid > students_grade[id].final) 47 students_grade[id].sum_grade = round(students_grade[id].mid * 0.4 + students_grade[id].final * 0.6); 48 else 49 students_grade[id].sum_grade = students_grade[id].final; 50 51 if (students_grade[id].online >= 200 && students_grade[id].sum_grade >= 60) //创建新的map来保存获取合格证书的学生 52 Certificate.insert(pair<string, grade>(id, students_grade[id])); 53 tmp_grade.final = -1; 54 } 55 56 vector< pair<string,grade> > v_sort(Certificate.begin(), Certificate.end()); //map无法用sort函数,于是转换为vector进行排序输出 57 sort(v_sort.begin(), v_sort.end(), cmp); 58 for (auto it = v_sort.begin(); it != v_sort.end(); ++it) //auto它不香嘛 59 cout << it->first << ' ' << it->second.online << ' ' 60 << it->second.mid << ' ' << it->second.final << ' ' << it->second.sum_grade << endl; 61 return 0; 62 }