1036 Boys vs Girls (25分)

本文介绍了一段使用C++编写的代码,该代码用于从输入的学生成绩中找出最高分的男生和女生,并计算他们的分数差。代码通过循环读取学生的姓名、性别、ID和分数,然后根据性别分别更新最高分男生和女生的信息。最后,程序输出最高分男女学生的信息及分数差。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>

using namespace std;

int main()
{
    int n;

    string girl_name, girl_id;  // 女生第一名的信息
    int girl_score;
    string boy_name, boy_id;  // 男生第一名的信息
    int boy_score;

    cin >> n;
    for (int i = 0; i < n; i ++ )
    {
        string name, sex, id;
        int score;
        cin >> name >> sex >> id >> score;  //不包含空格,若包含空格则用getline读入

        if (sex == "F")  // 如果当前同学是女生
        {
            if (girl_name.empty() || girl_score < score)
            {
                girl_name = name;
                girl_id = id;
                girl_score = score;
            }
        }
        else  // 男生
        {
            if (boy_name.empty() || boy_score > 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 (girl_name.size() && boy_name.size()) cout << abs(girl_score - boy_score) << endl;
    else cout << "NA" << endl;

    return 0;
}

思路:

  1. 每次读入直接判断男生女生,再判断是否要更新最高和最低成绩(若为空直接存)
  2. 男生女生都需要记录三个量:分数,id,姓名
  3. 判断string变量是否为空.empty()
  4. 输出先判断男女生是否存在,输出差值时判断男女生是否都存在(存在输出差值,否则输出NA)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值