问题标题: 酷町堂:9432

0
0
已解决
张盛铭
张盛铭
修练者
修练者

#include<iostream>
#include<algorithm>
using namespace std;
bool readStudentInfo(Student&student) {
    cin >> student.name >> student.strawberry >> student.cherry >> student.mulberry;
    if (student.strawberry < 0 || student.cherry < 0 || student.mulberry < 0) {
        cerr << "Error: Negative fruit count!" <<endl;
        return false;
    }
    student.total = student.strawberry + student.cherry + student.mulberry;
    return true;
}

// 按照总水果数从大到小对学生数组进行排序
bool cmpStudent(const Student& a, const Student& b) {
    return a.total > b.total;
}

int main() {
    int n;
    cin >> n;
    vector<Student> students(n);

    // 读取输入并计算每个学生的总水果数
    for (int i = 0; i < n; i++) {
        if (!readStudentInfo(students[i])) {
            // 如果输入不合法,跳过该学生的计算
            i--;
            n--;
        }
    }

    // 对学生数组进行排序
    sort(students.begin(), students.end(), cmpStudent);

    // 输出结果
    for (const auto& student : students) {
        cout << student.name << " " << student.total << endl;
    }

    return 0;
}


1
已采纳
林鄧天樂
林鄧天樂
初级光能
初级光能

用火车头

我要回答