C++ 代码实现学生成绩排序输出
#include
struct Student { //定义学生结构体 string id; //学号 int score; //成绩 }stu[100];
bool cmp(Student a, Student b) { //比较函数 if (a.score != b.score) return a.score > b.score; //成绩不同,按成绩从大到小排序 else return a.id < b.id; //成绩相同,按学号从小到大排序 }
int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> stu[i].id >> stu[i].score; } sort(stu, stu + n, cmp); //排序 for (int i = 0; i < n; i++) { if (stu[i].score >= 60) { //成绩及格,输出学号和成绩 cout << stu[i].id << ' ' << stu[i].score << endl; } else break; //成绩不及格,结束循环 } return 0; }
原文地址: https://www.cveoy.top/t/topic/oQSQ 著作权归作者所有。请勿转载和采集!