题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
#include<bits/stdc++.h> using namespace std; int flag; struct student { string name; int score; }; bool mycmp(student a, student b) { if (flag==0) { if (a.score >b.score) return 1; else return 0; } else { if (a.score >=b.score) return 0; else return 1; } } bool cmp(student a,student b){ if(flag==0) return a.score>b.score; else return a.score<b.score; //这里必须写else,否则牛客会编译失败 } int main() { int n; while (cin>>n) { cin >> flag; student stu[n]; for (int i = 0; i < n; i++) cin >> stu[i].name >> stu[i].score; stable_sort(&stu[0], &stu[n], mycmp); for (int i = 0; i < n; i++) cout << stu[i].name << ' ' << stu[i].score << endl; } return 0; }