PAT甲级:1036 Boys vs Girls (25分)

题干

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student's name, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference grade**Fgrade**M. If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.

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

思路

上sort函数,直接写一个符合题意的cmp函数,使得数组开头第一个就是女生最高分,最后一个是男生最低分。

然后就很简单了~

至于cmp函数怎么写,先比较性别,男生往后排,女生往前排。性别相同时按成绩从高到低排即可~

第二种方法也挺简单,就搜索一遍数组找到男女生的最大值和最小值也可以,复杂度应该比直接排序要小~

这种就不说了,比较简单,看看这种新奇点的方法能不能给自己带来点启发吧~

code

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct stu{
string name;
char sex;
string ID;
int grade;
};
bool cmp(stu a, stu b){//只需写一个cmp函数即可~
if(a.sex == b.sex) return a.grade > b.grade;
return a.sex < b.sex;
}
void P(stu s, char sex){
if(s.sex == sex) printf("%s %s\n", s.name.c_str(), s.ID.c_str());
else printf("Absent\n");
}
int main(int argc, char** argv) {
int n = 0, flag = 0;
scanf("%d", &n);
vector<stu> score(n);
for(int i = 0; i < n; i++) score[i].name.resize(15), score[i].ID.resize(15);
for(int i = 0; i < n; i++) scanf("%s %c %s %d", &score[i].name[0], &score[i].sex, &score[i].ID[0], &score[i].grade);
sort(score.begin(), score.end(), cmp);
if(!(score[0].sex == 'F' && score[score.size() - 1].sex == 'M')) flag = 1;
P(score[0], 'F');
P(score[score.size() - 1], 'M');
if(flag) printf("NA\n");
else printf("%d\n", score[0].grade - score[score.size() - 1].grade);
return 0;
}

结果

提交时间 状态 分数 题目 编译器 耗时 用户
2020/05/05 09:33:39 答案正确 25 1036 C++ (g++) 4 ms rash!
测试点 结果 耗时 内存
0 答案正确 3 ms 384 KB
1 答案正确 4 ms 384 KB
2 答案正确 3 ms 384 KB
3 答案正确 3 ms 384 KB

PAT甲级:1036 Boys vs Girls (25分)的更多相关文章

  1. PAT 甲级 1036 Boys vs Girls (25 分)(简单题)

    1036 Boys vs Girls (25 分)   This time you are asked to tell the difference between the lowest grade ...

  2. PAT Advanced 1036 Boys vs Girls (25 分)

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  3. PAT 1036 Boys vs Girls (25 分)

    1036 Boys vs Girls (25 分)   This time you are asked to tell the difference between the lowest grade ...

  4. 1036 Boys vs Girls (25分)(水)

    1036 Boys vs Girls (25分)   This time you are asked to tell the difference between the lowest grade o ...

  5. PAT甲级——1036 Boys vs Girls

    1036 Boys vs Girls This time you are asked to tell the difference between the lowest grade of all th ...

  6. PAT 1036 Boys vs Girls (25分) 比大小而已

    题目 This time you are asked to tell the difference between the lowest grade of all the male students ...

  7. 【PAT甲级】1036 Boys vs Girls (25 分)

    题意: 输入一个正整数N(题干没指出范围,默认1e5可以AC),接下来输入N行数据,每行包括一名学生的姓名,性别,学号和分数.输出三行,分别为最高分女性学生的姓名和学号,最低分男性学生的姓名和学号,前 ...

  8. PAT (Advanced Level) Practice 1036 Boys vs Girls (25 分)

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  9. PAT 甲级 1036 Boys vs Girls(20)

    https://pintia.cn/problem-sets/994805342720868352/problems/994805453203030016 This time you are aske ...

随机推荐

  1. ffmpeg architecture(下)

    ffmpeg architecture(下) 第3章-转码 TLDR:给我看代码和执行. $ make run_transcoding 我们将跳过一些细节,但是请放心:源代码可在github上找到. ...

  2. NEXTCLOUD 常见错误

    HTTP请求头"Strict-Transport-Security"没有配置为至少"15552000"秒出于增强安全性考虑推荐按照安全提示中的说明启用HSTS ...

  3. RabbitMQ由浅入深入门全总结(二)

    写在最前面 距离上一次发文章已经很久了,其实这段时间一直也没有停笔,只不过在忙着找工作还有学校结课的事情,重新弄了一下博客,后面也会陆陆续续会把文章最近更新出来~ 这篇文章有点长,就分了两篇 PS:那 ...

  4. UF_MTX 矩阵操作

    Open C   UF_MTX2_copyUF_MTX2_determinantUF_MTX2_identityUF_MTX2_initializeUF_MTX2_multiplyUF_MTX2_mu ...

  5. 使用VS code编写C++无法实时检测代码的解决办法

    更新:其实微软是有官方文档配置VS code 的C++的.地址是: https://code.visualstudio.com/docs/cpp 更改工作区后就发现不能再使用VS CODE愉快地写C+ ...

  6. 尼恩 Java高并发三部曲 [官方]

    高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三部曲 > 面试必备 + 大厂必备 + 涨薪 ...

  7. 如何使用 jest 和 lint-staged 只检测发生改动的文件

    我们现在在推进 EPC 的过程中,单元测试是必备的技能,在本地的 Git commit 之前进行单测非常有必要,总不能把所有的单测的压力都放在流水线上. 毕竟在流水线运行单测的成本还是挺高的,从 pu ...

  8. 安装nodejs版本模块报错notsup Unsupported platform for n

    使用npm install -g n报错 如果出现npm ERR! notsup Unsupported platform for n@6.7.0: wanted {"os":&q ...

  9. java入门了解、安装jdk及软件的选择

    ​ ​学习编程,一些必要的dos命令还是需要掌握的. 以下只是列出常用的: cd 目录路径: 进入一个目录 cd .. 进入父目录 dir 查看本目录下的文件和子目录列表 cls 清除屏幕命令 上下键 ...

  10. 20204107 孙嘉临 《PYTHON程序设计》实验四报告

    课程:<Python程序设计>班级: 2041姓名: 孙嘉临学号: 20204107实验教师:王志强实验日期:2020年6月29日必修/选修: 公选课 ##作为一个轻度游戏玩家,当然是要写 ...