Home Pat 1520 男孩和女孩
Post
Cancel

Pat 1520 男孩和女孩

题目

给定 N 个学生的成绩信息,请你求出女生第一名与男生倒数第一名的分数差距。

输入格式

第一行输入整数 N,表示学生数量。

接下来 N 行,每行包含一个学生的姓名,性别,ID和成绩。其中姓名和ID是长度不超过 10 且不包含空格的字符串。性别为 F(女)或 M(男)。成绩是一个范围在 [0,100] 的整数。保证所有学生的成绩互不相同。

输出格式

输出共三行。

第一行输出女生第一名的姓名和ID。

第二行输出男生倒数第一名的姓名和ID。

第三行输出女生第一名的成绩与男生倒数第一名的成绩的差的绝对值。

如果不存在某个性别的学生,则在对应行输出 Absent

在第三行输出 NA

数据范围

1≤N≤101

输入样例1:

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

输出样例1:

1
2
3
Mary EE990830
Joe Math990112
6

输入样例2:

1
2
1
Jean M AA980920 60

输出样例2:

1
2
3
Absent
Jean AA980920
NA

##


题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n;
    cin >> n;
    string f_name, m_name, f_id, m_id;
    int f_score = -1, m_score = 101;
    
    for (int i = 0; i < n; ++i) {
        string name, sex, id;
        int score;
        cin >> name >> sex >> id >> score;
        if (sex == "F") {
            if (score > f_score) {
                f_name = name;
                f_id = id;
                f_score = score;
            }
        } else {
            if (score < m_score) {
                m_name = name;
                m_id = id;
                m_score = score;
            }
        }
    }
    if (f_score != -1) {
        cout << f_name << ' ' << f_id << endl;
    } else {
        cout << "Absent" << endl;
    }
    
    if (m_score != 101) {
        cout << m_name << ' ' << m_id << endl;
    } else {
        cout << "Absent" << endl;
    }
    if (f_score == -1 || m_score == 101) {
        cout << "NA" << endl;
    } else {
        cout << abs(f_score - m_score) << endl;
    }
    return 0;
}

This post is licensed under CC BY 4.0 by the author.
Contents