题解 | #成绩排序#运用结构体排序
成绩排序
https://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7
#include <iostream> #include <algorithm> using namespace std; struct Student{ int id; //学号 int score; //分数 }; bool compare(Student s1, Student s2){ if(s1.score > s2.score){ return false; }else if(s1.score == s2.score && s1.id > s2.id){ return false; }else{ return true; } } int main() { int N;//学生的个数 scanf("%d", &N); Student arr[101]; for(int i = 0; i < N; i++){ scanf(("%d %d"), &arr[i].id, &arr[i].score); } sort(arr, arr + N, compare); for(int i = 0; i < N; i++){ printf("%d %d", arr[i].id, arr[i].score); printf("\n"); } }