输入
3
senpai 114 51 4
lxl 114 10 23
fafa 51 42 60
输出
senpai 114 51 4
#include<iostream>
#include<algorithm>
using namespace std;
struct Stu
{
string name;
int chi;
int math;
int eng;
int sum;
};
bool cmp(Stu a, Stu b)//降序
{
return a.sum > b.sum;
}
int main()
{
Stu stu[1005];
int n, sum = 0, max = 0, flag;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> stu[i].name >> stu[i].chi >> stu[i].math >> stu[i].eng;
stu[i].sum = stu[i].chi + stu[i].eng + stu[i].math;
}
sort(stu + 1, stu + 1 + n, cmp);
cout << stu[1].name << " " << stu[1].chi << " " << stu[1].math << " "<< stu[1].eng;
return 0;
}
sort函数:包含于#include的c++标准库中
sort(start,end,cmp)
参数
(1)start表示要排序数组的起始地址;
(2)end表示数组结束地址的下一位;
(3)cmp用于规定排序的方法,可不填,默认升序。
功能
sort函数用于C++中,对给定区间所有元素进行排序,默认为升序,也可进行降序排序。