题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7
#include <iostream> #include <map> using namespace std; struct score { int num; int score; }; score* bubleSort(score* s, int n ) { bool isSwap = false; int tempName, tempScore; for (int i = 0; i < n; i++) { //按照学生的成绩从小到大进行排序 for (int j = 0; j < n - 1 - i; j++) { if (s[j].score > s[j + 1].score) { tempScore = s[j].score; tempName = s[j].num; s[j].score = s[j + 1].score; s[j].num = s[j + 1].num; s[j + 1].score = tempScore; s[j + 1].num = tempName; isSwap = true; } } if (!isSwap) break; isSwap = false; } for (int i = 0; i < n; i++) { // 如果学生的成绩相同,则按照学号的大小进行从小到大排序。 for (int j = 0; j < n - 1 - i; j++) { if (s[j].score == s[j + 1].score) { if (s[j].num > s[j + 1].num) { tempScore = s[j].score; tempName = s[j].num; s[j].score = s[j + 1].score; s[j].num = s[j + 1].num; s[j + 1].score = tempScore; s[j + 1].num = tempName; isSwap = true; } } } if (!isSwap) break; isSwap = false; } return s; } int main() { int n; while (cin >> n) { auto* s = (score*)malloc(sizeof(score) * n); for (int i = 0; i < n; i++) { cin >> s[i].num >> s[i].score; } bubleSort(s, n); for (int i = 0; i < n; i++) { cout << s[i].num << " " << s[i].score << endl; } } } // 64 位输出请用 printf("%lld")