Sample Input 1:
3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95
Sample Output 1:
Mary EE990830
Joe Math990112
6
Sample Input 2:
1
Jean M AA980920 60
Sample Output 2:
Absent
Jean AA980920
NA
思路: 用一个vector存一下,按照分数排一下就好,按照要求输出即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
#define endl '\n'
struct node {
string name;
string gender;
string id;
int grade;
};
bool cmpm(node a, node b)
{
return a.grade < b.grade;
}
bool cmpf(node a, node b)
{
return a.grade > b.grade;
}
vector<node>f, m;
int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
string name;
string gender;
string id;
int grade;
for (int i = 1; i <= n; ++i)
{
cin >> name >> gender >> id >> grade;
if (gender[0] == 'F')
f.push_back({ name, gender, id, grade });
else if (gender[0] == 'M')
m.push_back({ name, gender, id, grade });
}
if (m.size() != 0)
sort(m.begin(), m.end(), cmpm);
if (f.size() != 0)
sort(f.begin(), f.end(), cmpf);
int flag = 0;
if (f.size() == 0)
{
cout << "Absent" << endl;
flag = 1;
}
else
{
cout << f[0].name << " " << f[0].id << endl;
}
if (m.size() == 0)
{
cout << "Absent" << endl;
flag = 1;
}
else
{
cout << m[0].name << " " << m[0].id << endl;
}
if (flag == 0)
{
cout << f[0].grade - m[0].grade << endl;
}
else
{
cout << "NA" << endl;
}
return 0;
}