求助,第一个点RE

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

Liquidice @ 2022-11-04 22:19:30

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct stu
{
    string name;
    int c=-1;
    int m=-1;
    int e=-1;
    int count=-1;
} num[1005];
int n;
int cmp(stu a, stu b)
{

    return a.count <= b.count;
}

int main(int argc, char const *argv[])
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> num[i].name >> num[i].c >> num[i].m >> num[i].e;
        num[i].count = num[i].c + num[i].m + num[i].e;
    }
    sort(num, num + n, cmp);
    cout << num[n - 1].name << " " << num[n - 1].c << " " << num[n - 1].m << " " << num[n - 1].e << endl;
    system("pause");
    return 0;
}

by Serendi_Pity @ 2022-11-06 20:14:20

@Liquidice 其实可以不用结构体

#include<bits/stdc++.h>
using namespace std;
string a[1001];
int b[1001],c[1001],d[1001],e[1001],s,f,n,ans=1;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i]>>b[i]>>c[i]>>d[i];
        e[i]=b[i]+c[i]+d[i];
    }
    for(int j=1;j<=n;j++){
        if(e[j] > e[ans]) ans = j;
    }
    cout<<a[ans]<<" "<<b[ans]<<" "<<c[ans]<<" "<<d[ans];
}

by Liquidice @ 2022-11-07 15:52:52

@sjz520 感谢大佬


|