【PAT甲级 - C++题解】1036 Boys vs Girls

简介: 【PAT甲级 - C++题解】1036 Boys vs Girls

1036 Boys vs Girls


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 gradeF−gradeM. 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


思路

  1. 这道题可以边输入边判断,用几个变量来存储对应的最大值和最小值。
  2. 最后输出的时候要注意男生和女生的个数。

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    string girl_name, girl_id;
    int girl_score;
    string boy_name, boy_id;
    int boy_score;
    for (int i = 0; i < n; i++)
    {
        string name, gender, id;
        int score;
        cin >> name >> gender >> id >> score;
        if (gender == "F") //如果是女生
        {
            if (girl_name.empty() || score > girl_score)
            {
                girl_name = name;
                girl_id = id;
                girl_score = score;
            }
        }
        else    //如果是男生
        {
            if (boy_name.empty() || score < boy_score)
            {
                boy_name = name;
                boy_id = id;
                boy_score = score;
            }
        }
    }
    //判断是否有女生
    if (girl_name.empty())   puts("Absent");
    else    cout << girl_name << " " << girl_id << endl;
    //判断是否有男生
    if (boy_name.empty())    puts("Absent");
    else    cout << boy_name << " " << boy_id << endl;
    //判断男生女生是否都存在,如果存在就输出成绩差值
    if (boy_name.size() && girl_name.size()) cout << girl_score - boy_score << endl;
    else    cout << "NA" << endl;
    return 0;
}


目录
相关文章
|
6天前
|
算法 前端开发 大数据
【C/C++ 基础知识 】C++中易混淆的函数和关键字:std::find vs std::search,std::remove vs std::erase,remove vs delete
【C/C++ 基础知识 】C++中易混淆的函数和关键字:std::find vs std::search,std::remove vs std::erase,remove vs delete
36 0
|
6天前
|
算法 编译器 C语言
【C/C++ 编译器的差异化】C++标准库在GCC和VS之间的表现差异
【C/C++ 编译器的差异化】C++标准库在GCC和VS之间的表现差异
174 1
|
6天前
|
算法 编译器 C语言
【C++ 函数 基本教程 第六篇 】深度解析C++函数符号:GCC与VS的名称修饰揭秘
【C++ 函数 基本教程 第六篇 】深度解析C++函数符号:GCC与VS的名称修饰揭秘
53 1
|
6天前
|
Linux C++
(C++)VS下sizeof(string(““))与linux-g++下sizeof(string(““))大小区别及原因剖析
(C++)VS下sizeof(string(““))与linux-g++下sizeof(string(““))大小区别及原因剖析
37 0
(C++)VS下sizeof(string(““))与linux-g++下sizeof(string(““))大小区别及原因剖析
|
5月前
|
Java 程序员 C++
C++ vs Python vs Java
C++ vs Python vs Java
33 0
|
6月前
|
JSON C++ 数据格式
《C++避坑神器·二十二》VS能正常运行程序,但运行exe程序无响应解决办法
《C++避坑神器·二十二》VS能正常运行程序,但运行exe程序无响应解决办法
72 0
|
6月前
|
安全 测试技术 C++
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
最近用到了gRPC,配置了很长时间,分享一下配置过程。先来看一下我准备的文件包(资源我放在最后)
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
|
8月前
|
IDE 编译器 开发工具
善用 vs 中的错误列表和输出窗口,高效查找 C++ 多工程编译错误
善用 vs 中的错误列表和输出窗口,高效查找 C++ 多工程编译错误
|
8月前
|
C++ 计算机视觉 Python
C++ VS OpenGL绘制教室三维立体旋转图像
C++ VS OpenGL绘制教室三维立体旋转图像
80 0
C++ VS OpenGL绘制教室三维立体旋转图像
|
8月前
|
C++ Python
C++ VS Open3D点云显示颜色渲染滤波
C++ VS Open3D点云显示颜色渲染滤波
99 0