WA#1求助

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

ikuncdl @ 2024-08-18 18:00:19

大佬们看看

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
using namespace std;
int n;
struct oi{
    string na;
    int c,m,e,z;
}a[10005];
bool cmp(oi x,oi y)
{
    return x.z > y.z;
}
int main()
{
    cin>>n;
    for (int i = 1;i <= n;i++)
    {
        cin>>a[i].na>>a[i].c>>a[i].m>>a[i].e;
        a[i].z = a[i].c + a[i].m + a[i].e;
    }
    sort(a + 1,a + n + 1,cmp);
    cout<<a[1].na<<" "<<a[1].c<<" "<<a[1].m<<" "<<a[1].e;
    return 0;
}

by jza110228 @ 2024-08-18 18:02:59

是输出顺序的问题吗(没试)

#include<bits/stdc++.h>
using namespace std;
int main(){
    string name[1005];
    int c[1005],d[1005],e[1005];
    int n,max=-1,t;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>name[i]>>c[i]>>d[i]>>e[i];
    }
    for(int i=0;i<n;i++){
        if(c[i]+d[i]+e[i]>max){
            max=c[i]+d[i]+e[i];
            t=i;
        }
    }
    cout<<name[t]<<" "<<c[t]<<" "<<d[t]<<" "<<e[t];
    return 0;
}

by Alex_smy @ 2024-08-18 18:06:56

@ikuncdl 如果有多个总分相同的学生,输出靠前的那位。所以结构体里要加上一个 id


by Alex_smy @ 2024-08-18 18:09:13

@ikuncdl

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
using namespace std;
int n;
struct oi{
    string na;
    int id,c,m,e,z;
}a[10005];
bool cmp(oi x,oi y)
{
    if(x.z==y.z) return x.id<y.id;
    return x.z > y.z;
}
int main()
{
    cin>>n;
    for (int i = 1;i <= n;i++)
    {
        cin>>a[i].na>>a[i].c>>a[i].m>>a[i].e;
        a[i].z = a[i].c + a[i].m + a[i].e;
        a[i].id=i;
    }
    sort(a + 1,a + n + 1,cmp);
    cout<<a[1].na<<" "<<a[1].c<<" "<<a[1].m<<" "<<a[1].e;
    return 0;
}

by Alex_smy @ 2024-08-18 18:09:45

@ikuncdl 求关


by ikuncdl @ 2024-08-18 18:24:24

@jza110228 谢谢


by ikuncdl @ 2024-08-18 18:26:33

@Alex_smy 谢谢大佬AC了


by ikuncdl @ 2024-08-18 18:27:03

@Alex_smy 已关


by ikuncdl @ 2024-08-18 18:27:24

此帖结


|