题目大意:给出若干名学生的成绩,分为GP, GM和GF,最终成绩G按照给定的规则计算。要求按照G降序排名,G相同的按照id升序。
阅读理解题,符合输出条件的是GP>=200以及G>=60,注意不是GF>=60,因此只有在计算出G之后才能判断是否符合输出条件。
AC代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
struct student
{
string id;
int gp;
int gm;
int gf;
double g;
student():gp(-1), gm(-1), gf(-1){};
bool operator <(const student &other)
{
if(this->g != other.g) return this->g > other.g;
else return this->id < other.id;
}
};
int main()
{
int P, M, N;
map<string, student> mp;
scanf("%d%d%d", &P, &M, &N);
for (int i = 0; i < P + M + N; ++i)
{
char name[30];
int grade;
scanf("%s%d", name, &grade);
string id = string(name);
if(i < P && grade >= 200)
{
mp[id].id = id;
mp[id].gp = grade;
}
else if(i < P + M)
{
mp[id].id = id;
mp[id].gm = grade;
}
else
{
mp[id].id = id;
mp[id].gf = grade;
}
}
vector<student> v;
for(map<string, student>::iterator it = mp.begin(); it != mp.end(); it++)
{
if(it->second.gp >= 200)
{
it->second.g = (it->second.gm > it->second.gf) ? (int)round(it->second.gm * 0.4 + it->second.gf * 0.6) : it->second.gf;
if(it->second.g >= 60) v.push_back(it->second);
}
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); ++i)
{
printf("%s %d %d %d %d\n", v[i].id.c_str(), v[i].gp, v[i].gm, v[i].gf, (int)v[i].g);
}
return 0;
}