EternityForMe @ 2024-09-01 10:16:27
#include<bits/stdc++.h>
using namespace std;
struct S{
string name;
int c,m,e;
int zf(){
return c+m+e;
}
}s[10001];
bool cmp(S x,S y){
return x.zf()<y.zf();
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>s[i].name>>s[i].c>>s[i].m>>s[i].e;
}
sort(s,s+n,cmp);
cout<<s[0].name<<" "<<s[0].c<<' '<<s[0].m<<' '<<s[0].e<<' ';
return 0;
}
求大佬帮忙
by adsd45666 @ 2024-09-01 10:25:14
如果有多个总分相同的学生,输出靠前的那位。
by adsd45666 @ 2024-09-01 10:36:11
重构结构体,则AC code:
#include<bits/stdc++.h>
using namespace std;
struct S{
string name;
int c,m,e,id;
int zf(){
return c+m+e;
}
}s[10001];
bool cmp(S x,S y){
if(x.zf()==y.zf()) return x.id<y.id;
return x.zf()>y.zf();
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
s[i].id=i;
cin>>s[i].name>>s[i].c>>s[i].m>>s[i].e;
}
sort(s,s+n,cmp);
cout<<s[0].name<<" "<<s[0].c<<' '<<s[0].m<<' '<<s[0].e<<' ';
return 0;
}
by adsd45666 @ 2024-09-01 10:36:33
@EternityForMe