1036 Boys vs Girls

题目来源:PAT (Advanced Level) Practice

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

words: 

male 男性的        female 女性的        distinct 明显的,有区别的

思路:

1. 根据题目给出的 n 个学生的信息----姓名,性别,ID,成绩;找出女生中成绩最高的学生在第一行输出姓名和ID,不存在则输出Absent;找出男生中成绩最低的学生在在第二行输出姓名和ID,不存在则输出Absent;在第三行输出女生最高成绩和男生最低成绩的成绩差,若有一者不存在则输出NA;

//PAT ad 1036 Boys vs Girls
#include <iostream>
using namespace std;

struct student
{
	string name;
	char gender;
	string id;
	int grade;
}p[2];

int main()
{
	int n,i;
	bool pm=false,pf=false;  //最高分和最低分存在的标记 
	int mi=100,ma=0;
	
	string name;
	char gender;
	string id;
	int grade;
	
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>name>>gender>>id>>grade;
		if(gender=='F'&&grade>ma)   //女生中的最高分 
		{
			ma=grade;pf=true;
			p[0].name=name;p[0].gender=gender;p[0].id=id;p[0].grade=grade;
		}
		else if(gender=='M'&&grade<mi)  //男生中的最低分
		{
			mi=grade;pm=true;
			p[1].name=name;p[1].gender=gender;p[1].id=id;p[1].grade=grade;
		}
		
	}
	if(pf)	cout<<p[0].name<<" "<<p[0].id<<endl;   //最高分存在 
	else	cout<<"Absent"<<endl;
	if(pm)	cout<<p[1].name<<" "<<p[1].id<<endl;   //最低分存在
	else	cout<<"Absent"<<endl;
	if(pf&&pm)	cout<<p[0].grade-p[1].grade<<endl;  //最高分和最低分都存在 
	else	cout<<"NA"<<endl;
	
	
	return 0;
}

上一篇:Spring Boot学习笔记10——配置国际化页面


下一篇:1036 Boys vs Girls (25 分)