AcWing 429. 奖学金
原题链接
简单
作者:
HiCode_001
,
2019-11-05 23:03:32
,
所有人可见
,
阅读 718
考察知识点:排序、关键字排序
时间复杂度:0(nlogn) sort()函数为快速排序
有两中排序方式
1)运算符重载
2)algorithm函数库中sort的cmp自定义排序 [参考链接](https://www.cnblogs.com/yspworld/p/4328590.html)
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 310;
struct Person{
int chinese,math,english;
int total,id;
}person[N];
bool cmp(const Person& a,const Person& b){
if(a.total==b.total){
if(a.chinese==b.chinese){
return a.id<b.id;
}else{
return a.chinese>b.chinese;
}
}else{
return a.total>b.total;
}
}
int main(){
int n;
cin >> n;
for(int i = 1; i<=n; i++) {
int chinese,math,english;
cin >> chinese >> math >> english;
int total = chinese+math+english;
person[i] = {chinese,math,english,total,i};
}
sort(person+1,person+n+1,cmp);
for(int i = 1; i<=5; i++) cout << person[i].id <<" " << person[i].total <<endl;
return 0;
}