90分求助,第一个测试点没过

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

2207bug @ 2024-03-30 17:30:57

#include<iostream>
#include<algorithm>
using namespace std;

struct Student{
    string name;
    int chinese;
    int math;
    int english;
};

bool cmp(const Student& s1, const Student& s2){
    if((s1.chinese+s1.math+s1.english) > (s2.chinese+s2.math+s2.english)){
        return true;
    }else{
        return false;
    }
}

int n;
Student s[1000];

int main()
{
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> s[i].name >> s[i].chinese >> s[i].math >> s[i].english;
    }
    sort(s, s+n, cmp);
    cout << s[0].name << " " << s[0].chinese << " " << s[0].math << " " << s[0].english << endl;
    return 0;
}

by syman_liu @ 2024-03-30 17:58:52

看这里

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

by syman_liu @ 2024-03-30 17:59:20

AC code

#include <bits/stdc++.h>
using namespace std;

int n;

struct stu
{
    string name;
    int ch,ma,en,sum,id;
    void read(int i)
    {
        cin >> name >> ch >> ma >> en;
        sum = ch + ma + en,id = i;
    }
    void write()
    {
        cout << name << " " << ch << " " << ma << " " << en << endl;
    }
}a[1005];

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

int main()
{
    cin >> n;
    for (int i = 1;i <= n;i ++){
        a[i].read(i);
    }
    sort(a + 1,a + n + 1,cmp);
    a[1].write();
    return 0;
}

by syman_liu @ 2024-03-30 17:59:44

加一个id即可


by syman_liu @ 2024-03-30 18:00:07

求关注(厚颜无耻)


|