第一个题解!!
2025-05-05 09:32:21
发布于:上海
25阅读
0回复
0点赞
第一个题解!!
废话不多说,直接上代码
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
// 定义学生结构体
struct Student {
string name;
int acCount;
int timeScore;
// 构造函数,用于初始化学生信息
Student(string n) : name(n), acCount(0), timeScore(0) {}
};
// 比较函数,用于对学生进行排序
bool compareStudents(const Student& a, const Student& b) {
if (a.acCount != b.acCount) {
return a.acCount > b.acCount;
}
if (a.timeScore != b.timeScore) {
return a.timeScore < b.timeScore;
}
return a.name < b.name;
}
int main() {
int n, m;
cin >> n >> m;
vector<Student> students;
string name;
// 读取每个学生的信息
while (cin >> name) {
Student student(name);
for (int i = 0; i < n; ++i) {
string status;
cin >> status;
// 检查是否有错误提交次数
size_t pos = status.find('(');
if (pos != string::npos) {
int time = stoi(status.substr(0, pos));
int penalty = stoi(status.substr(pos + 1, status.find(')') - pos - 1));
student.acCount++;
student.timeScore += time + penalty * m;
} else if (status[0] != '-' && status[0] != '0') {
// 没有错误提交,直接通过
student.acCount++;
student.timeScore += stoi(status);
}
}
students.push_back(student);
}
// 对学生进行排序
sort(students.begin(), students.end(), compareStudents);
// 输出排名结果
for (const auto& student : students) {
cout << left << setw(10) << student.name << " "
<< right << setw(2) << student.acCount << " "
<< right << setw(4) << student.timeScore << endl;
}
return 0;
}
求赞
这里空空如也
有帮助,赞一个