90分求助

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

_SB_ @ 2024-12-19 15:59:24

请求大佬修改一下我的破烂代码

第一个wa

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct chengji
{
    string name;
    long long int Chinese,maths,Eglish,sum=0;
}stu[1000];
bool cmp(chengji a,chengji b)
{
    if(a.sum!=b.sum) return a.sum>b.sum;
    else return a.sum<b.sum;
}
int main()
{
    long long int n,i;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>stu[i].name>>stu[i].Chinese>>stu[i].maths>>stu[i].Eglish;
        stu[i].sum=stu[i].Chinese+stu[i].maths+stu[i].Eglish;
    }
    sort(stu,stu+n,cmp);
    cout<<stu[0].name<<' '<<stu[0].Chinese<<' '<<stu[0].maths<<' '<<stu[0].Eglish;
    return 0;
}

by kaenoly @ 2024-12-19 16:09:05

@SB 试一下不用sort


by kaenoly @ 2024-12-19 16:12:32

#include<bits/stdc++.h>
using namespace std;
struct s
{
    string name;
    int chinese,maths,English,number;
}s[1001];
int main()
{
    int a,ma,n;
    cin>>a;
    for(int i=1;i<=a;i++)
    {
        s[i].number=i;
        cin>>s[i].name>>s[i].chinese>>s[i].maths>>s[i].English;
    }
    ma=s[1].chinese+s[1].maths+s[1].English;
    for(int i=1;i<=a;i++)
    {
        if(s[i].chinese+s[i].maths+s[i].English>ma)
        {
            ma=s[i].chinese+s[i].maths+s[i].English;
            n=i;
        }
    }
    cout<<s[n].name<<" "<<s[n].chinese<<" "<<s[n].maths<<" "<<s[n].English;
}

by _SB_ @ 2024-12-19 16:17:56

@kaenoly
感谢感谢


by _CCCCcccc @ 2024-12-21 10:16:55

@kaenoly能不能用swap


|