分析
大致思路是:输入+处理1;输出+处理2
粗处理:注意找到数组最大的边界
赋值-1给期中成绩(在线和期末是不需要,原因是在输入时已经筛选了)
处理1:输入时;根据输入给的顺序(在线,期中)进行筛选
在输入期末时,此时同时计算总评,并插入set进行排序
处理2:判断是否总评大于60
输出按照期末的递减顺序;若相同,则按照学生id升序排列
其他
四舍五入:通过对结果+0.5,以保证截断时为四舍五入
cmp比较函数必须写到struct中去,同时内部用operation、const
注:set和map都是只能比较 键(给定结构中的成员变量),不能比较 值 或者 调用外部的函数来获取遍历
代码
#include <bits/stdc++.h>
#include <unordered_map>
#pragma warning(disable:4996)
#pragma warning(disable:4700)
#pragma warning(disable:4703)
using namespace std;
struct node {
string name;
int score;
node(string x, int a, int b) {
name = x;
a > b ? score = (int)(a * 0.4 + b * 0.6 + 0.5) : score = b;
}
};
struct cmp {
bool operator()(node x, node y)const {
if (x.score > y.score)
return true;
if (x.score == y.score) {
if (x.name < y.name)
return true;
}
return false;
}
};
int online_n, mid_n, final_n, max_size;
set<node,cmp>print;
vector<int>online_score, mid_score, final_score;
map<string, int>name_id;
void InPut();
int main() {
InPut();
int id;
for (auto i : print) {
if (i.score >= 60) {
cout << i.name;
id = name_id[i.name];
printf(" %d %d %d %d\n", online_score[id], mid_score[id], final_score[id], i.score);
}
}
return 0;
}
void InPut() {
string x;
int y, max_size, id{ 0 };
map<string, int>::iterator it;
scanf("%d %d %d", &online_n, &mid_n, &final_n);
max_size = max(online_n, mid_n);
max_size = max(max_size, final_n);
online_score.resize(max_size);
mid_score.resize(max_size);
fill(mid_score.begin(), mid_score.end(), -1);
final_score.resize(max_size);
for (int i = 0; i < online_n; ++i) {
cin >> x;
scanf("%d", &y);
if (y >= 200) {
online_score[id] = y;
name_id[x] = id;
id++;
}
}
for (int i = 0; i < mid_n; ++i) {
cin >> x;
scanf("%d", &y);
it = name_id.find(x);
if (it != name_id.end()) {
mid_score[it->second] = y;
}
}
for (int i = 0; i < final_n; ++i) {
cin >> x;
scanf("%d", &y);
it = name_id.find(x);
if (it != name_id.end()) {
final_score[it->second] = y;
print.insert({ x, mid_score[it->second], final_score[it->second] });
}
}
}