Dream2353 @ 2023-04-17 20:52:31
#include<bits/stdc++.h>
using namespace std;
struct good{
string id;
int ma,ch,en,s;
};
good a[1005];
bool cmp(good x,good y){
if(x.s!=y.s)
return x.s>y.s;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].id>>a[i].ch>>a[i].ma>>a[i].en;
a[i].s=a[i].ma+a[i].ch+a[i].en;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=1;i++)
cout<<a[i].id<<" "<<a[i].ch<<" "<<a[i].ma<<" "<<a[i].en;
return 0;
}
by 1ooper @ 2023-05-02 19:25:00
不用for(int i=1;i<=1;i++),直接把他删掉,然后在struct结构体里面写一个int xu;为他的序号,然后把cmp改一下:
if(x.s==y.s)return x.xu>y.xu;
return x.s>y.s;
然后再到main中的for输入里面,写一个:
a[i].xu=i;
by 1ooper @ 2023-05-02 20:13:33
把那个最后输出的a[i]改成a[0]
by 1ooper @ 2023-05-03 22:54:19
a[1]