题解 | 成绩排序
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string name;
int score;
};
bool OrderDown(Student stu1, Student stu2) {
if (stu1.score < stu2.score) {
return false;
} else if (stu1.score == stu2.score) {
return false;
}
return true;
}
bool OrderUp(Student stu1, Student stu2) {
if (stu1.score > stu2.score) {
return false;
} else if (stu1.score == stu2.score) {
return false;
}
return true;
}
int main() {
int n;
scanf("%d", &n);
int op;
scanf("%d", &op);
vector<Student> stu_vc;
for (int i = 0; i < n; i++) {
Student stu;
char c[1024];
scanf("%s%d", c, &stu.score);
stu.name = c;
stu_vc.push_back(stu);
}
if (op) { //1、按成绩升序
stable_sort(stu_vc.begin(), stu_vc.end(), OrderUp);
} else { //0、按成绩降序
stable_sort(stu_vc.begin(), stu_vc.end(), OrderDown);
}
for(int i=0;i<n;i++){
printf("%s %d\n",stu_vc[i].name.c_str(),stu_vc[i].score);
}
}
// 64 位输出请用 printf("%lld")
查看25道真题和解析