智能成绩表

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

struct Student {string name;vector<int> scores;int totalScore;

Student(string n, vector<int> s) : name(n), scores(s), totalScore(0) {
    for (int score : scores) {
        totalScore += score;
    }
}

};

bool compareStudents(Student& a, Student& b) {

if (a.totalScore != b.totalScore) {return a.totalScore > b.totalScore;}else {return a.name < b.name;}

}

int main() {int n, m;cin >> n >> m;

vector<string> subjects(m);
for (int i = 0; i < m; ++i) {
    cin >> subjects[i];
}

vector<Student> students;
for (int i = 0; i < n; ++i) {
    string name;
    cin >> name;

    vector<int> scores(m);
    for (int j = 0; j < m; ++j) {
        cin >> scores[j];
    }

    students.push_back(Student(name, scores));
}

string rankSubject;
cin >> rankSubject;

int rankIdx = -1;
for (int i = 0; i < m; ++i) {
    if (subjects[i] == rankSubject) {
        rankIdx = i;
        break;
    }
}

if (rankIdx == -1) {
    sort(students.begin(), students.end(), compareStudents);
}
else {
    sort(students.begin(), students.end(), [rankIdx](const Student& a, const Student& b) {
        if (a.scores[rankIdx] != b.scores[rankIdx]) {
            return a.scores[rankIdx] > b.scores[rankIdx];
        }
        else {
            return a.name < b.name;
        }
        });
}

for (const auto& student : students) {
    cout << student.name << " ";
}

return 0;

}

#机械/制造每日一题#
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务