题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
#include<iostream> #include <iomanip> #include <algorithm> using namespace std; typedef struct student { string name; int grade; int squeue; } student; bool compare1(student a, student b) { if (a.grade != b.grade) { return a.grade < b.grade; } else { return a.squeue < b.squeue; } } bool compare2(student a, student b) { if (a.grade != b.grade) { return a.grade > b.grade; } else { return a.squeue < b.squeue; } } int main() { int n = 0, way = 0; while (::scanf("%d %d", &n, &way) != EOF) { student cla[n]; for (int i = 0; i < n; ++i) { cin >> cla[i].name >> cla[i].grade; cla[i].squeue = i; } if (way == 1) { sort(cla, cla + n, compare1); } else { sort(cla, cla + n, compare2); } for (int i = 0; i < n; ++i) { cout << cla[i].name << " " << cla[i].grade << endl; } } return 0; }