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 ...
随机推荐
- 5G和AI机器人平台为工业4.0和无人机提供服务
5G和AI机器人平台为工业4.0和无人机提供服务 Qualcomm 5G and AI robotics platform delivers for Industry 4.0 and drones 高 ...
- python+selenium基础篇,句柄操作(多个页面切换)
1.我们打开网页有时候会开出多个页面,导致我们常常定位不到我们需要的元素,这种情况可能就是句柄的因素,如下图所示: 2.句柄切换代码如下: from selenium import webdriver ...
- seldom 2.0 让接口自动化测试更简单
前言 HTTP接口测试很简单,不管工具.框架.还是平台,只要很的好的几个点就是好工具. 测试数据问题:比如删除接口,重复执行还能保持结果一致,必定要做数据初始化. 接口依赖问题:B接口依赖A的返回值, ...
- WordPress安装篇(5):源码编译安装LNMP并部署WordPress
与YUM方式安装相比,源码编译安装方式更灵活,安装过程中能自定义功能和参数,特别是在批量部署服务器又要求软件版本及配置一致时,源码编译安装的优势很明显.本文介绍如何通过源码编译方式安装Nginx1.1 ...
- Java线程的并发工具类
Java线程的并发工具类. 一.fork/join 1. Fork-Join原理 在必要的情况下,将一个大任务,拆分(fork)成若干个小任务,然后再将一个个小任务的结果进行汇总(join). 适用场 ...
- Redis与DB的数据一致性解决方案(史上最全)
文章很长,而且持续更新,建议收藏起来,慢慢读! 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三 ...
- 有了Java8的“+”真的可以不要StringBuilder了吗
最近在头条上看到一篇帖子,说Java8开始,字符串拼接时,"+"会被编译成StringBuilder,所以,字符串的连接操作不用再考虑效率问题了,事实真的是这样吗?要搞明白,还是要 ...
- 复习Spring第四课---Spring对国际化的支持
其实国际化这东西很少使用,之前也就是粗略的学了下,趁今天有空,拿出来稍微写写.以前学android开发的时候,类似于多语言的版本.差别就是一个是手机打开,一个是浏览器打开,本质是一样的. 在Sprin ...
- 『心善渊』Selenium3.0基础 — 8、使用CSS选择器定位元素
目录 1.CSS选择器介绍 2.CSS选择器定位语法 3.Selenium中使用CSS选择器定位元素 (1)通过属性定位元素 (2)通过标签定位元素 (3)通过层级关系定位元素 (4)通过索引定位元素 ...
- 温故知新,.Net Core遇见Digital Signature(MD5/RSA/SM),微服务签名机制设计
什么是数字签名(Digital Signature) 数字签名(Digital Signature)是只有信息的发送者才能产生的别人无法伪造的一段数字串,这段数字串同时也是对信息的发送者发送信息真实性 ...