90分第一个测试用例过不去,求dalao看看

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

XXYNLY @ 2024-10-13 17:19:49


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

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

struct student {
    string name;
    int chinese=0, math=0, english=0;
}a,ans;

int main() {

    int n; cin >> n;
    for (int i = 1; i <=n; i++) {
        cin >> a.name >> a.chinese >> a.math >> a.english;
        if (a.chinese + a.math + a.english > ans.chinese + ans.english + ans.math) {
            ans = a;
        }
    }
    cout << ans.name << ' ' << ans.chinese << ' ' << ans.math << ' ' << ans.english << endl;

    return 0;
}

by IAKIOI___ @ 2024-10-13 17:30:10

@XXYNLY

#include <iostream>
#include <algorithm>
using namespace std;
struct Monkey
{
    int c;
    int m;
    int e;
    int z;
    string id;
    int iv;
    void read(int k)
    {
        cin >> id >> c >> m >> e;
        z = c + m + e;
        iv = k;
    }
};
Monkey m1[1005];
bool cmp(Monkey x, Monkey y)
{
    if (x.z != y.z) return x.z > y.z;
    return x.iv < y.iv;
}
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        m1[i].read(i);
    }
    sort(m1 + 1, m1 + n + 1, cmp);
    cout << m1[1].id << " " << m1[1].c << " " << m1[1].m << " " << m1[1].e << endl;
    return 0;
}

by xlp0922 @ 2024-10-13 19:25:01

你没有考虑所有人分数都等于0的情况,ans的Chinese可以先设成-1


|