PAT (Advanced Level) Practice 1036 Boys vs Girls (25 分) 凌宸1642

PAT (Advanced Level) Practice 1036 Boys vs Girls (25 分) 凌宸1642

题目描述:

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.

译:每个输入文件包含一个测试用例。每一种情况包含一个正整数 N,后面跟着 N 行学生信息。每一行包含一个学生的姓名、性别、ID和年级,以空格分隔,其中 nameID 为不超过10个字符的字符串,不带空格,性别F(女性)或M(男性),成绩是 0 到 100 之间的整数。保证所有的成绩都是不同的。


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 gradeF−gradeM. If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.

译:对于每个测试用例,输出3行。第一行给出了最高分女生的姓名和ID,第二行给出了最低分男生的姓名和ID。第三行给出了不同的gradeF - grade。如果缺少一个这样的学生,则在相应的行中输出 Absent,并在第三行中输出NA。。


Sample Input 1 (样例输入 1 ):

3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

Sample Output 1 (样例输出 1 ):

Mary EE990830
Joe Math990112
6

Sample Input 2 (样例输入 2 ):

1
Jean M AA980920 60

Sample Output 2 (样例输出 2 ):

Absent
Jean AA980920
NA

The Idea:

这题的思路很简单,根据男生女生的性别作为区分,建立两个 map , 由于题目中说明了 所有的 分数都是不一样的,所以以分数作为 map 的 key 很合适,由于输出的时候需要输出 name 和 ID ,所以 map 用 pair<string , string> 作为 map 的 value 。然后 map 是有序的 , 不带第三个参数 greater (默认 less) 表示按照key 的升序排列 , 而带上 greater 表示按照 key 的降序排列,这样对于男生的最低分和女生的最高分,我们都只需要取 第一个元素即可 。 对于判空的情况,只需根绝对应 map 的size() 是否为 0 即可。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
int n ;
map<int , pair<string , string> , greater<int> > female ; // 按 key 降序排列的 map
map<int , pair<string , string> , greater<int> >::iterator it1 ;// 按key降序排列的map的迭代器
map<int , pair<string , string> , less<int> > male ;// 按 key 升序排列的 map
map<int , pair<string , string> , less<int> >::iterator it2 ;// 按key升序排列的map的迭代器
int main(){
	string name , num ;
	char sex ;
	int grade ;
	scanf("%d" , &n) ;
	for(int i = 0 ; i < n ; i ++){
		cin >> name >> sex >> num >> grade ; // 输入 姓名 性别 ID 成绩
		if(sex == 'F') female[grade] = make_pair(name , num) ; // 如果是女生 ,加入female 
		else male[grade] = make_pair(name , num) ; // 是男生 加入 male
	}
	if(female.size() == 0) printf("Absent\n") ; // 不存在女生
	else{
		it1 = female.begin() ; // 输出女生的最高成绩
		printf("%s %s\n", ((it1->second).first).c_str(),((it1->second).second).c_str()) ;
	}
	if(male.size() == 0) printf("Absent\n") ; // 不存在男生
	else{
		it2 = male.begin() ; // 输出男生的最低分数
		printf("%s %s\n", ((it2->second).first).c_str(),((it2->second).second).c_str()) ;
	}
	if(female.size() == 0 || male.size() == 0) printf("NA\n") ; // 不同时存在男生女生
	else printf("%d\n" , it1 -> first - it2 -> first ) ; // 输出 女生最好成绩和男生最差成绩差值
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingchen0522

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值