Online Judge PTA 学生成绩处理

C++实现学生成绩处理

学生成绩处理

描述

已知有5个学生,学生信息由3门课的成绩(C语言、英语和数学)构成,
求出每个学生的总成绩,并按总成绩降序排序后输出。

输入

输入以空格隔开的5个学生的成绩。

输出

输出5个学生的3门课成绩和总成绩,并按总成绩降序排序。

输出时每个数据之间有一个空格,成绩均保留1位小数。

输入样例 1 

93 91 89
60.5 72 75
50 60.5 63
85 91.5 50
80 81 82.5

输出样例 1

93.0 91.0 89.0 273.0
80.0 81.0 82.5 243.5
85.0 91.5 50.0 226.5
60.5 72.0 75.0 207.5
50.0 60.5 63.0 173.5

提示

HINT 时间限制:200ms 内存限制:64MB

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>

struct Student {
    double c_language;
    double english;
    double math;
    double total;

    // 计算总成绩
    void calculateTotal() {
        total = c_language + english + math;
    }
};

// 自定义排序函数
bool compare(const Student& a, const Student& b) {
    return a.total > b.total; // 降序
}

int main() {
    std::vector<Student> students(5);

    // 输入成绩
    for (int i = 0; i < 5; ++i) {
        std::cin >> students[i].c_language >> students[i].english >> students[i].math;
        students[i].calculateTotal(); // 计算总成绩
    }

    // 排序
    std::sort(students.begin(), students.end(), compare);

    // 输出结果
    for (const auto& student : students) {
        std::cout << std::fixed << std::setprecision(1)
                  << student.c_language << " "
                  << student.english << " "
                  << student.math << " "
                  << student.total << std::endl;
    }

    return 0;
}

如有侵权,联系删除

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Flocx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值