AcWing
  • 首页
  • 题库
  • 题解
  • 分享
  • 问答
  • 活动
  • 应用
  • 吐槽
  • 登录/注册

AcWing 1520. 男孩 vs 女孩    原题链接    简单

作者: 作者的头像   王小强 ,  2021-02-22 10:30:54 ,  阅读 15


1


结构体+排序解法

#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;
}

0 评论

你确定删除吗?

© 2018-2021 AcWing 版权所有  |  京ICP备17053197号-1
联系我们  |  常见问题
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息