学生成绩处理
描述
已知有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;
}
C++实现学生成绩处理
1万+

被折叠的 条评论
为什么被折叠?



