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 ...
 
随机推荐
- Mac下安装及配置Appium环境
			
candiceli Mac下安装及配置Appium环境 我是小白,自己研究appium好几周了. 一开始按照同事这篇文章设置Mac下的环境,http://www.cnblogs.com/tangd ...
 - 【接口测试】-1.常用的接口测试工具(Postman、soupUI、Jemeter)
			
1) Postman 1.get/post请求-- postman获取用户信息1 get方式:可以直接在url中写入参数 Post方式:请求体可以写到URL或Body的form-data中写参 ...
 - 温故知新,.Net Core遇见JWT(JSON Web Token)授权机制方案
			
什么是JWT JWT (JSON Web Token) 是一个开放标准,它定义了一种以紧凑和自包含的方法,用于在双方之间安全地传输编码为JSON对象的信息. 因此,简单来说,它是JSON格式的加密字符 ...
 - Windows10 准备/安装Flutter研发环境
			
Flutter简介 Flutter 是 Google 开源的 UI 工具包,帮助开发者通过一套代码库高效构建多平台精美应用,支持移动.Web.桌面和嵌入式平台. Flutter 中国 安装Flutte ...
 - 01 Linux系统配置初始化
			
#/bin/bash export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin # 更改主机名 # hostnam ...
 - 15、nginx编译安装
			
15.1.nginx介绍: 1.Nginx是一个开源,支持高性能,高并发的www服务和代理服务软件,由俄罗斯人开发,是开源的,nginx因具有高并发(特别是静态的 资源).占用系统资源少等特性,且功能 ...
 - 7、基本数据类型(tuple)
			
7.1.tuple类: 1.元组元素用小括号括起来,用逗号分割每个元素,一般写元组的时候,推荐在最后加入逗号,该 逗号不占元素位置,目的是为了方便识别: tu = (111, "alex&q ...
 - 1.6Java语言规范、API、JDK、和IDE
			
要点提示:Java语言规范定义了Java的语法,Java库则在JavaAPI中定义.JDK是用于开发和运行Java程序的软件.IDE是快速开发程序的集成开发环境. 计算机语言有严格的使用规范.
 - 使用RestTemplate发送post请求,请求头中封装参数
			
最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败.中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...
 - let  const关键字
			
let 是取代var 在块级作用域里面有效 <!DOCTYPE html> <html lang="en"> <head> <meta c ...