[PAT A1036]Boys vs Girls

[PAT A1036]Boys vs Girls

题目描述

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.

输入格式

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.

输出格式

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.

输入样例1

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

输出样例1

Mary EE990830
Joe Math990112
6

输入样例2

1
Jean M AA980920 60

输出样例2

Absent
Jean AA980920
NA

解析

  1. 题目的意思就是首先输入N,给出N个同学的信息,然后接下来世N行,代表n个同学,输入他们的姓名,性别(用M表示男生,F表示女生),ID和成绩
  2. 输出格式就是第一行输出成绩最高的女生的姓名和ID,第二行输出成绩最低的男生的姓名和ID,最后第三行输出成绩最高的女生的成绩减去成绩最低的男生的成绩,如果不存在男生的话,就输出Absent,同理女生也一样,如果两者中至少有一个为空,那么成绩差输出NA
  3. 这次为了简化运算,我没有使用结构体将男生和女生结构化,而是在输入的过程中就解决了问题,这是上次A1006题([PAT A1006]Sign In and Sign Out)带给我的启发。
#include<iostream>
#include<string>
#include<climits>    //添加这个头文件是为了使用INT_MAX和INT_MIN
using namespace std;
int main()
{
 int N;
 string name, id;  //存放每个输入的人的姓名,ID
 char gender;      //存放每个输入的人的性别
 int grade, m_low = INT_MAX, f_high = INT_MIN;   //m_low存放男生最低成绩,f_high存放女生最高成绩
 string m_id, f_id, m_name, f_name; //存放男生ID,女生ID,男生姓名,女生姓名以便输出
 cin >> N;
 while (N--)
 {
  cin >> name >> gender >> id >> grade;
  if (gender == 'M')
  {
   if (grade < m_low)
   {
    m_low = grade;
    m_name = name;
    m_id = id;
   }
  }
  else
  {
   if (grade > f_high)
   {
    f_high = grade;
    f_name = name;
    f_id = id;
   }
  }
 }
 if (f_high == INT_MIN) cout << "Absent" << endl;
 else
 {
  cout << f_name << " " << f_id << endl;
 }
 if (m_low == INT_MAX) cout << "Absent" << endl;
 else
 {
  cout << m_name << " " << m_id << endl;
 }
 if (m_low == INT_MAX || f_high == INT_MIN) cout << "NA";
 else cout << f_high - m_low;
 return 0;
}

水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出宝贵的意见!

猜你喜欢

转载自blog.csdn.net/qq_38293932/article/details/88087750