Files
python/C++专题课程/排序/对结构体快速排序.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

31 lines
678 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
using namespace std;
//学生的结构体
typedef struct student {
char name[20];
int math;
int english;
} Student;
//函数声明
bool cmp(Student a,Student b);
int main() {
//先按math从大到小排序,math相等,按english从大到小排序
Student a[4]= {{"xiaoming",67,89},{"limei",90,56},{"zhaosi",90,99}};
sort(a,a+3,cmp);
for(int i=0; i<3; i++)
cout<<a[i].name <<" "<<a[i].math <<" "<<a[i].english <<endl;
return 0;
}
//函数实现
bool cmp(Student a,Student b) {
if(a.math <b.math )
return a.math <b.math ;//按math从大到小排序
else if(a.math ==b.math )
return a.english>b.english ; //math相等,按endlish从大到小排序23
}