PAT甲级:1036 Boys vs Girls (25分)
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**F−grade**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分)的更多相关文章
- PAT 甲级 1036 Boys vs Girls (25 分)(简单题)
1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade ...
- 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 ...
- PAT 1036 Boys vs Girls (25 分)
1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade ...
- 1036 Boys vs Girls (25分)(水)
1036 Boys vs Girls (25分) This time you are asked to tell the difference between the lowest grade o ...
- 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 ...
- PAT 1036 Boys vs Girls (25分) 比大小而已
题目 This time you are asked to tell the difference between the lowest grade of all the male students ...
- 【PAT甲级】1036 Boys vs Girls (25 分)
题意: 输入一个正整数N(题干没指出范围,默认1e5可以AC),接下来输入N行数据,每行包括一名学生的姓名,性别,学号和分数.输出三行,分别为最高分女性学生的姓名和学号,最低分男性学生的姓名和学号,前 ...
- 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 ...
- PAT 甲级 1036 Boys vs Girls(20)
https://pintia.cn/problem-sets/994805342720868352/problems/994805453203030016 This time you are aske ...
随机推荐
- JS使用Enter事件将输入的字符倒叙输出
在JavaScript中执行当用户按下Enter键位时将用户输入的字符倒叙输出! HTML代码: <body> <form id="form1" runat=&q ...
- 使用JavaScript获取url(request)中的参数
这次是使用JavaScript来获取url(request)中的参数 在日常页面编写的过程中为了方便操作在<script>中通过使用window.location.href="要 ...
- Hadoop 数据迁移用法详解
数据迁移使用场景 冷热集群数据分类存储,详见上述描述. 集群数据整体搬迁.当公司的业务迅速的发展,导致当前的服务器数量资源出现临时紧张的时候,为了更高效的利用资源,会将原A机房数据整体迁移到B机房的, ...
- JUC 并发编程--05, Volatile关键字特性: 可见性, 不保证原子性,禁止指令重排, 代码证明过程. CAS了解么 , ABA怎么解决, 手写自旋锁和死锁
问: 了解volatile关键字么? 答: 他是java 的关键字, 保证可见性, 不保证原子性, 禁止指令重排 问: 你说的这三个特性, 能写代码证明么? 答: .... 问: 听说过 CAS么 他 ...
- 『言善信』Fiddler工具 — 11、Fiddler中Composer功能详解
目录 1.Composer功能介绍 2.Composer界面说明 3.使用方式 (1)自定义Request请求 (2)Composer重复发送请求 (3)Composer篡改请求数据 1.Compos ...
- 实验2、Flask模板、表单、视图和重定向示例
实验内容 1. 实验内容 表单功能与页面跳转功 能是Web应用程序的基础功能,学习并使用他们能够更好的完善应用程序的功能.Flask使用了名为Jinja2的模板引擎,该引擎根据用户的交互级别显示应用程 ...
- 08:'my_tag' is not a registered tag library. Must be one of
确保每次修改模板标签时都重新启动 Django 开发服务器(或确保它自己重新启动).如果服务器没有重新启动,Django 将不会注册标签. 从 django 1.9 开始,您可以在如下设置中加载这些新 ...
- 《吃透MQ系列》之扒开Kafka的神秘面纱
大家好,这是<吃透 MQ 系列>的第二弹,有些珊珊来迟,后台被好几个读者催更了,实属抱歉! 这篇文章拖更了好几周,起初的想法是:围绕每一个具体的消息中间件,不仅要写透,而且要控制好篇幅,写 ...
- 一起来踩踩 Spring 中这个循环依赖的坑
1. 前言 2. 典型场景 3. 什么是依赖 4. 什么是依赖调解 5. 为什么要依赖注入 6. Spring的依赖注入模型 7. 非典型问题 参考资料 1. 前言 这两天工作遇到了一个挺有意思的Sp ...
- AVAssetWriter视频数据编码
AVAssetWriter介绍 可以通过AVAssetWriter来对媒体样本重新做编码. 针对一个视频文件,只可以使用一个AVAssetWriter来写入,所以每一个文件都需要对应一个新的AVAss ...