以下是一个C++程序,实现了上述的排序规则: ```cpp #include #include #include #include using namespace std; struct Student { string name; int chinese; int math; int english; int total; Student(string n, int c, int m, int e) : name(n), chinese(c), math(m), english(e), total(c + m + e) {} }; bool compareStudents(const Student& a, const Student& b) { if (a.total != b.total) { return a.total > b.total; // 按总分降序排序 } else if (a.chinese != b.chinese) { return a.chinese > b.chinese; // 如果总分相等,按语文成绩降序排序 } else if (a.math != b.math) { return a.math > b.math; // 如果语文成绩相等,按数学成绩降序排序 } else { return a.name < b.name; // 如果数学成绩也相等,按姓名字典序升序排序 } } int main() { int n; cin >> n; vector students(n); for (int i = 0; i < n; ++i) { string name; int chinese, math, english; cin >> name >> chinese >> math >> english; students[i] = Student(name, chinese, math, english); } sort(students.begin(), students.end(), compareStudents); for (const auto& student : students) { cout << student.name << " " << student.chinese << " " << student.math << " " << student.english << " " << student.total << endl; } return 0; } ``` 该程序首先定义了一个`Student`结构体,用于存储每个学生的姓名和各科成绩。然后,定义了一个`compareStudents`函数,用于比较两个学生的成绩。这个函数首先比较总分,如果总分不同,则按总分降序排序;如果总分相同,则比较语文成绩,按语文成绩降序排序;如果语文成绩也相同,则比较数学成绩,按数学成绩降序排序;最后,如果数学成绩也相同,则按姓名字典序升序排序。 在`main`函数中,首先读入学生人数`n`,然后读入每个学生的姓名和各科成绩,并将这些信息存储到`students`向量中。接着,使用`sort`函数和`compareStudents`函数对`students`向量进行排序。最后,遍历排序后的`students`向量,输出每个学生的姓名和各科成绩。

点赞(0)
×
关注公众号,登录后继续创作
或点击进入高级版AI
扫码关注后未收到验证码,回复【登录】二字获取验证码
发表
评论
返回
顶部