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 ...
随机推荐
- 用Taro写一个微信小程序(三)—— 配置dva
一.关于dva dva 首先是一个基于 redux 和 redux-saga 的数据流方案,然后为了简化开发体验,dva 还额外内置了 react-router 和 fetch,所以也可以理解为一个轻 ...
- Spring Cloud06: Ribbon 负载均衡
一.使用背景 前面的学习中,我们已经使用RestTemplate来实现了服务消费者对服务提供者的调用,如果在某个具体的业务场景下,对某个服务的调用量突然大幅提升,这个时候就需要对该服务实现负载均衡以满 ...
- SpringBoot基础系列之自定义配置源使用姿势实例演示
[SpringBoot基础系列]自定义配置源的使用姿势介绍 前面一篇博文介绍了一个@Value的一些知识点,其中提了一个点,@Value对应的配置,除了是配置文件中之外,可以从其他的数据源中获取么,如 ...
- 八、Nginx的TCP/UDP调度器
nginx 1.9后才可以调用其他应用 1.9前只能调用web 部署nginx服务器----配置----起服务.验证 部署nginx服务器: [root@proxy ~]# yum –y instal ...
- kafka基础知识梳理
一.Kafka的基本概念 关键字: 分布式发布订阅消息系统:分布式的,分区的消息服务 Kafka是一种高吞吐量的分布式发布订阅消息系统,使用Scala编写. 对于熟悉JMS(Java Message ...
- NOIP模拟测试39,思维禁锢专场「工业题·玄学题·卡常题」
工业题 题解 抱歉,题解没时间写了 代码 #include<bits/stdc++.h> using namespace std; #define ll long long #define ...
- 『心善渊』Selenium3.0基础 — 5、XPath路径表达式详细介绍
目录 1.XPath介绍 2.什么是XML 3.XML与HTML对比 4.为什么使用XPath定位页面中的元素 5.XPath中节点之间的关系 (1)节点的概念 (2)节点之间的关系类型 6.XPat ...
- 【LeetCode每日一题 Day 5】5. 最长回文子串
大家好,我是编程熊,今天是LeetCode每日一题的第五天,一起学习LeetCode第五题<最长回文子串>. 题意 给你一个字符串 s,找到 s 中最长的回文子串. 示例 输入:s = & ...
- 堆和栈的内存分布&一些关于内存泄露、栈溢出和野指针的内容(头秃
内存泄漏&栈溢出 C++中,我们主要涉及的内存是栈和堆, 堆 (By programmer) 申请后由程序员主动释放,遗忘后果严重: 栈 (By compiler)需要时由编译器分配,在不需 ...
- redis-cluster集群安装(windows)
在此先奉上安装包(链接:https://pan.baidu.com/s/1QHYQPkYPuiRWhdj9APbjnw 提取码:jv8x ) 1. 安装ruby 下载 rubyinstaller-2. ...