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. Usb-type-C端口实现的挑战与设计方案

    Usb-type-C端口实现的挑战与设计方案 USB Type-C port implementation challenges and design solutions USB from 1.1 t ...

  2. 简化可视SLAM应用程序的开发

    简化可视SLAM应用程序的开发 Easing the development of visual SLAM applications 同步定位和映射(SLAM)描述了一个设备(如机器人)使用传感器数据 ...

  3. 【NX二次开发】Block UI 选择节点

    属性说明 属性   类型   描述   常规           BlockID    String    控件ID    Enable    Logical    是否可操作    Group    ...

  4. SpringBoot2配置文件application.yaml

    源码基于SpringBoot 2.4.4 1.认识配置文件 1.1 配置文件的加载 创建SpringBoot项目的时候,会自动创建一个application.properties文件,该文件是Spri ...

  5. Java必学MySQL数据库应用场景

    Java教程分享Java必学之MySQL数据库应用场景,在当前的后台开发中,MySQL应用非常普遍,企业在选拔Java人才时也会考察求职者诸如性能优化.高可用性.备份.集群.负载均衡.读写分离等问题. ...

  6. Linkerd 2.10(Step by Step)—将 GitOps 与 Linkerd 和 Argo CD 结合使用

    Linkerd 2.10 系列 快速上手 Linkerd v2.10 Service Mesh(服务网格) 腾讯云 K8S 集群实战 Service Mesh-Linkerd2 & Traef ...

  7. leetcode1141 N*3矩阵。阿里笔试no.1

    你有一个 n x 3 的网格图 grid ,你需要用 红,黄,绿 三种颜色之一给每一个格子上色,且确保相邻格子颜色不同(也就是有相同水平边或者垂直边的格子颜色不同). 给你网格图的行数 n . 请你返 ...

  8. 分布式事务与Seate框架(3)——Seata的AT模式实现原理

    前言 在上两篇博文(分布式事务与Seate框架(1)--分布式事务理论.分布式事务与Seate框架(2)--Seata实践)中已经介绍并实践过Seata AT模式,这里一些例子与概念来自这两篇(特别是 ...

  9. Linux网络命令与脚本使用

    作为系统管理员,经常需要诊断和解决网络问题,而配置.监控与保护网络有助于发现问题并在事情范围扩大前得意解决,并且网络的性能与安全也是管理与诊断网络的重要部分.这里总结一下常用与Linux网络管理的命令 ...

  10. Go语言中的有缓冲channel和无缓冲channel区别

    Go语言中的有缓冲channel和无缓冲channel区别 结论 ch1:=make(chan int)// 无缓冲 ch2:=make(chan int,1)// 有缓冲 无缓冲: 当向ch1中存值 ...