80分求助

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

frankfan7707 @ 2022-05-12 17:39:55

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

#include<bits/stdc++.h>
using namespace std;
struct student{
    string name;
    int chinese;
    int maths;
    int english;
    int score;
};
bool cmp(student a,student b){
    if(a.score==b.score)return a.name<b.name;
    else return a.score>b.score;
}
int n;
student s[10005];
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s[i].name>>s[i].chinese>>s[i].maths>>s[i].english;
        s[i].score=s[i].chinese+s[i].maths+s[i].english;
    }
    sort(s,s+n,cmp);
    cout<<s[0].name<<" "<<s[0].chinese<<" "<<s[0].maths<<" "<<s[0].english;
} 
/*
*  ┏┓    ┏┓+ +
* ┏┛┻━━━━━━━┛┻┓ + +
* ┃      ┃
* ┃  ━    ┃ ++ + + +
*  ████  ━  ████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃      ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃      ┣┓
*    ┃      ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

请忽视代码末尾请大佬们帮忙看看哪里出现了问题,感谢!


by barbatoss @ 2022-05-12 17:49:30

别管龙哥了,别管龙哥了


by barbatoss @ 2022-05-12 17:50:22

如果有多个总分相同的学生,输出靠前的那位。但你按照名字字典序排了,所以会WA。


by barbatoss @ 2022-05-12 17:52:55

代码差不多这样

#include<bits/stdc++.h>
using namespace std;
struct student{
    string name;
    int chinese;
    int maths;
    int english;
    int score;
    int id;
};
bool cmp(student a,student b){
    if(a.score==b.score)return a.id<b.id;//按顺序排而不是name字典序
    else return a.score>b.score;
}
int n;
student s[10005];
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s[i].name>>s[i].chinese>>s[i].maths>>s[i].english;
        s[i].score=s[i].chinese+s[i].maths+s[i].english;s[i].id=i;
    }
    sort(s,s+n,cmp);
    cout<<s[0].name<<" "<<s[0].chinese<<" "<<s[0].maths<<" "<<s[0].english;
}

by barbatoss @ 2022-05-12 17:53:04

@frankfan7707


by frankfan7707 @ 2022-05-12 18:17:11

@barbatoss 感谢!已AC。


|