原题地址点 这里
总结
- 要利用结构体组织数据
- 结构内重载 < 运算符可用 STL 的 sort 排序,注意函数是 const 的。
- 灵活运用引用简化代码。
代码
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn= 305;
int size;
struct Student{
int id;
int chinese;
int math;
int english;
int sum;
bool operator<(const Student& s)const{
if(this->sum != s.sum) return this->sum > s.sum;
if(this->chinese != s.chinese) return this->chinese > s.chinese;
return this->id < s.id;
}
};
Student s[maxn];
int main(){
scanf("%d", &size);
for(int i=1; i<=size; ++i){
Student &now= s[i];
now.id= i;
scanf("%d%d%d\n", &now.chinese, &now.math, &now.english);
now.sum= now.chinese+now.math+now.english;
}
sort(s+1, s+1+size);
for(int i=1; i<=5; ++i){
Student &now= s[i];
printf("%d %d\n", now.id, now.sum);
}
return 0;
}