HIT2023112282 @ 2023-10-17 20:22:53
#include<bits/stdc++.h>
using namespace std;
struct Stu
{
string name;
int chi,math,eng;
int sum;
}a[1010];
bool cmp(Stu x,Stu y)
{
return x.sum>=y.sum;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].name>>a[i].chi>>a[i].math>>a[i].eng;
a[i].sum=a[i].chi+a[i].eng+a[i].math;
}
sort(a+1,a+1+n,cmp);
cout<<a[1].name<<" "<<a[1].chi<<" "<<a[1].math<<" "<<a[1].eng;
return 0;
}
by Pitiless_boy @ 2023-10-18 20:51:31
@HIT2023112282 哦,好的
by HIT2023112282 @ 2023-10-18 20:53:17
@Argvchs 我这样写的话遵循了严格大小关系,但还是有问题
#include<bits/stdc++.h>
using namespace std;
struct Stu
{
string name;
int chi,math,eng;
int sum;
}a[1010];
bool cmp(Stu x,Stu y)
{
return x.sum<y.sum;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].name>>a[i].chi>>a[i].math>>a[i].eng;
a[i].sum=a[i].chi+a[i].eng+a[i].math;
}
sort(a+1,a+1+n,cmp);
cout<<a[n].name<<" "<<a[n].chi<<" "<<a[n].math<<" "<<a[n].eng;
return 0;
}
by Argvchs @ 2023-10-18 21:08:43
@HIT2023112282 加上 id 排序啊
by HIT2023112282 @ 2023-10-19 20:32:31
我理解了,谢谢