90分,求调!

P5740 【深基7.例9】最厉害的学生

auto_lambda @ 2023-08-29 08:56:42

#include<iostream>
#include<algorithm>
const int N = 1010;
using namespace std;
struct stu{
     string name;
     int Ch;
     int math;
     int English;
     int sum ;
     bool operator<(stu&s){
          return sum>s.sum;
     }
}a[N];
int n;

int main()
{
    cin>>n;
    for(int i = 0;i<n;i++){
         cin>>a[i].name>>a[i].Ch>>a[i].math>>a[i].English;
         a[i].sum = a[i].Ch+a[i].math+a[i].English;
    }

    sort(a,a+n);
    cout<<a[0].name<<' '<<a[0].Ch<<' '<<a[0].math<<' '<<a[0].English<<endl;
}

by zacharyzhong @ 2023-08-29 09:00:49

#include<iostream>
#include<algorithm>
const int N = 1010;
using namespace std;
struct stu{
     string name;
     int Ch;
     int math;
     int English;
     int sum ,id;
}a[N];
int n;

 bool operator < (stu x,stu y){
      if(x.sum!=y.sum) return x.sum>y.sum;
      else return x.id<y.id;
 }

int main()
{
    cin>>n;
    for(int i = 0;i<n;i++){
         cin>>a[i].name>>a[i].Ch>>a[i].math>>a[i].English;
         a[i].sum = a[i].Ch+a[i].math+a[i].English;a[i].id = i;
    }

    sort(a,a+n);
    cout<<a[0].name<<' '<<a[0].Ch<<' '<<a[0].math<<' '<<a[0].English<<endl;
}

by zacharyzhong @ 2023-08-29 09:01:41

@auto_lambda 本蒟蒻实在不会用结构体内的函数,就把函数放外面了,而且这题要求了:输出最靠前的那个


by auto_lambda @ 2023-08-29 09:05:07

@Zacharyzhong 感谢感谢,没想到重载<做不到,还是得自己写cmp函数


|