90分,求调

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

SBcv_pp @ 2024-12-17 19:38:30


#include<iostream>
#include<string> 
using namespace std;
struct student{
    string name;
    int chinese,maths,english;
}a,b;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a.name>>a.chinese>>a.maths>>a.english; 
        if(a.chinese+a.maths+a.english>b.chinese+b.maths+b.english){
            b=a;
        }
    }
    cout<<b.name<<" "<<b.chinese<<" "<<b.maths<<" "<<b.english<<endl;
    return 0;
}

by acbdsyj @ 2024-12-20 21:17:54

你这种写法要特判全部总分一样的情况,要不然b拿不到答案

#include<iostream>
#include<string> 
using namespace std;
struct student
{
    string name;
    int chinese,maths,english;
}a,b,c;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n;cin>>n;
    bool flag=false;
    for(int i=1;i<=n;i++)
    {
        cin>>a.name>>a.chinese>>a.maths>>a.english; 
        if(i==1)c.name=a.name,c.chinese=a.chinese,c.maths=a.maths,c.english=a.english;
        if(a.chinese+a.maths+a.english>b.chinese+b.maths+b.english){
            b=a;flag=true;
        }
    }
    if(!flag)cout<<c.name<<" "<<c.chinese<<" "<<c.maths<<" "<<c.english<<endl;
    else cout<<b.name<<" "<<b.chinese<<" "<<b.maths<<" "<<b.english<<endl;
    return 0;
}

你代码改的 个人建议最好用stable_sort(),简单快捷


|