C++ 代码
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 310;
//定义一个结构体方便对三课数据进行维护
struct Person
{
int chinese, math, english;
int total;
int id;
// 重载< 目的是为了在使用sort函数的时候告诉sort函数如何排序
// 因为std::sort(RandomAccessIterator first, RandomAccessIterator last,
// Compare comp) 有三个参数
// 重载< 其实就等于传入了第三个参数
bool operator< (const Person& W)const
{
if (total != W.total) return total > W.total;
if (chinese != W.chinese) return chinese > W.chinese;
return id < W.id;
}
}person[N];
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
{
int chinese, math, english;
scanf("%d%d%d", &chinese, &math, &english);
int total = chinese + math + english;
person[i] = {chinese, math, english, total, i};
}
// 由于循环是从1开始,所以我们需要从我们对数组排序也需要从1开始所以需要person + 1
sort(person + 1, person + 1 + n);
for (int i = 1; i <= 5; i ++ ) printf("%d %d\n", person[i].id, person[i].total);
return 0;
}
python代码
# https://www.acwing.com/solution/content/30004/
if __name__ == "__main__":
n = int(input())
res = []
for i in range(1, n + 1):
c, m, e = map(int, input().split())
res.append([c + m + e, c, i])
# 对元素的第三个字段进行排序,负号是为了总分和语文成绩进行升序排序,而x[2]是为了进行降序排序
res.sort(key=lambda x: (-x[0], -x[1], x[2]))
for i in range(5):
print(res[i][-1], res[i][0])