智能成绩表

#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;

}

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

相关推荐

07-15 16:52
已编辑
门头沟学院 Java
周五投的,流程今天结束
投递地平线等公司7个岗位
点赞 评论 收藏
分享
07-02 10:39
门头沟学院 Java
Steven267:说点真实的,都要秋招了,还没有实习,早干嘛去了,本来学历就差,现在知道急了,而且你这个简历完全可以写成一页,劣势太大了,建议转测试
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务