90分求助,第一个用例过不了!!!

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

yangbin1 @ 2024-02-24 00:46:46

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

struct student
{
    char name[10];
    int C;
    int M;
    int E;
    int sum;
}S[1000];
bool cmp(student a,student b){
    return a.sum>b.sum;
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>S[i].name>>S[i].C>>S[i].M>>S[i].E;
        S[i].sum=S[i].C+S[i].M+S[i].E;
    }
    sort(S,S+n,cmp);
    cout<<S[0].name<<" "<<S[0].C<<" "<<S[0].M<<" "<<S[0].E<<endl;
    return 0;
}

by yueyan_WZF @ 2024-02-24 08:17:24

《 用 例 》


by yueyan_WZF @ 2024-02-24 08:18:31

题目中有这样的一句话,你是不是没看见:“如果有多个总分相同的学生,输出靠前的那位。”


by yueyan_WZF @ 2024-02-24 08:20:33

@yangbin1 AC代码如下:

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

struct student
{
    char name[10];
    int C;
    int M;
    int E;
    int sum;
    int id;
}S[1000];
bool cmp(student a,student b){
    if(a.sum==b.sum) return a.id<b.id; 
    return a.sum>b.sum;
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>S[i].name>>S[i].C>>S[i].M>>S[i].E;
        S[i].sum=S[i].C+S[i].M+S[i].E;
        S[i].id=i;
    }
    sort(S,S+n,cmp);
    cout<<S[0].name<<" "<<S[0].C<<" "<<S[0].M<<" "<<S[0].E<<endl;
    return 0;
}

把判断语句放cmp里了


by yangbin1 @ 2024-02-24 11:31:13

@yueyan_WZF @yueyan_WZF 感谢感谢感谢


by Zsh20040530 @ 2024-03-04 20:14:32

@yangbin1 @yueyan_WZF 可以麻烦请问一下吗,bool类型的函数里面,返回的那个id是什么呀,为什么要加入这个变量呢


|