90分求助!

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

dwxweishengpu @ 2024-11-17 21:32:24

#include<bits/stdc++.h>
using namespace std;
struct Student{
    string name;
    int chinese,math,english,ans;
}a[1000];
bool cmp(Student a,Student b){
    if (a.ans != b.ans) {
        return a.ans > b.ans;
    }
    else{
        return a.ans < b.ans;
    } 
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].name>>a[i].chinese>>a[i].math>>a[i].english;
        a[i].ans=a[i].chinese+a[i].math+a[i].english;
    }
    sort(a+1,a+n+1,cmp);
    cout<<a[1].name<<" "<<a[1].chinese<<" "<<a[1].math<<" "<<a[1].english;
    return 0;
}

by meifan666 @ 2024-11-17 21:34:58

@dwxweishengpu 如果有多个总分相同的学生,输出靠前的那位。


by Hiris @ 2024-11-17 21:56:10

@dwxweishengpu

bool cmp(Student a,Student b){
    if (a.ans != b.ans) {
        return a.ans > b.ans;
    }
    else{
        return a.ans < b.ans;
    } 
}

如果a.ans==b.ans a.ans>b.ans不成立 会调换a b


by dwxweishengpu @ 2024-11-18 20:32:50

@meifan666@Hiris 感谢大佬回复!


|