新手光能
错误代码:
#include<iostream>
#include<iomanip>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
struct stu{
string name;
int yi,er,san;
int sum(){
return yi+er+san;
}
}a[45];
bool cmp(stu x,stu y){
return x.sum()>x.sum();
}
int n;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].name>>a[i].yi>>a[i].er>>a[i].san;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
cout<<a[i].name<<endl;
}
return 0;
}
曹子浩在2023-01-30 12:57:11追加了内容
更改后:
#include<iostream>
#include<iomanip>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
struct stu{
string name;
int id,yi,er,san;
int sum(){
return yi+er+san;
}
}a[45];
bool cmp(stu x,stu y){
return x.sum()>x.sum();
}
int n;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].name>>a[i].id>>a[i].yi>>a[i].er>>a[i].san;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
cout<<a[i].name<<endl;
}
return 0;
}