AcWing 1520. 男孩 vs 女孩
原题链接
简单
作者:
王小强
,
2021-02-22 10:30:54
,
所有人可见
,
阅读 332
结构体+排序解法
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 105;
int n;
string _name, _id;
char _sex;
int _score;
struct Student {
string id, name;
char sex;
int score;
bool operator<(const Student& o) const {
return score < o.score;
}
} students[N], *boy, *girl;
int main(void) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
cin >> _name >> _sex >> _id >> _score;
students[i] = {id : _id, name : _name, sex : _sex, score : _score};
}
sort(students + 1, students + n + 1);
for (int i = 1; i <= n; ++i) {
if (students[i].sex == 'M') {
boy = &students[i];
break;
}
}
for (int i = n; i >= 1; --i) {
if (students[i].sex == 'F') {
girl = &students[i];
break;
}
}
if (girl) printf("%s %s\n", girl->name.c_str(), girl->id.c_str());
else puts("Absent");
if (boy) printf("%s %s\n", boy->name.c_str(), boy->id.c_str());
else puts("Absent");
if (boy && girl) printf("%d\n", abs(girl->score - boy->score));
else puts("NA");
return 0;
}