题目链接https://pintia.cn/problem-sets/994805342720868352/problems/994805345401028608
题目大意:给出三张表,分别记录了一堆人的【id和作业成绩】【id和期中成绩】【id和期末成绩】。总评成绩的计算是期中和期末成绩加权求和。要求【作业成绩>=200】且【总评>=60】才能得到证书。求最后得到证书的人的信息的排序。排序先按总评成绩降序,再按id升序。
思路:id是distinct的,直接上map读就完事。读完了遍历,能拿证书的放进vector里。最后排序、输出。但是要注意回车键,记得把回车键用getchar()
吃掉再cin
读id
for (int i = 0; i < P; i++) {
cin >> id >> score;
getchar();
sheet[id].p = score;
}
for (int i = 0; i < M; i++) {
cin >> id >> score;
getchar();
sheet[id].mid = score;
}
for (int i = 0; i < N; i++) {
cin >> id >> score;
getchar();
sheet[id].final = score;
}
完整代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;
class Grade{
public:
int p, mid, final, total;
Grade(){p = mid = final = total = -1;}
};
class Node {
public:
string id;
int total;
Node(){}
Node(string id, int total):id(id), total(total){}
};
bool cmp(Node x, Node y) {
if (x.total != y.total)
return x.total > y.total;
else
return x.id < y.id;
}
int main() {
int P, M, N, score;
string id;
scanf("%d %d %d\n", &P, &M, &N);
map<string, Grade> sheet;
vector<Node> list;
for (int i = 0; i < P; i++) {
cin >> id >> score;
getchar();
sheet[id].p = score;
}
for (int i = 0; i < M; i++) {
cin >> id >> score;
getchar();
sheet[id].mid = score;
}
for (int i = 0; i < N; i++) {
cin >> id >> score;
getchar();
sheet[id].final = score;
}
for (auto it = sheet.begin(); it != sheet.end(); it++) {
if (it->second.p >= 200) {
if (it->second.mid > it->second.final)
it->second.total = round(.4*it->second.mid + .6*it->second.final);
else
it->second.total = it->second.final;
if (it->second.total >= 60)
list.push_back(Node(it->first, it->second.total));
}
}
sort(list.begin(), list.end(), cmp);
for (int i = 0; i < list.size(); i++)
cout << list[i].id << " " << sheet[list[i].id].p << " " << sheet[list[i].id].mid << " "
<< sheet[list[i].id].final << " " << list[i].total << endl;
return 0;
}