有没有大佬调一下代码 90分第一个点没过

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

yzh___nb @ 2023-08-25 09:08:49

#include<bits/stdc++.h>
using namespace std; 
int main()
{
    int n,yuwen=0,shuxue=0,yingyu=0,zf=0,maxzf,maxyw,maxsx,maxyy;
    string name,maxname;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>name>>yuwen>>shuxue>>yingyu;
        zf=yuwen+shuxue+yingyu;
        if(zf>maxzf)
        {
            maxzf=zf;
            maxname=name;
            maxyw=yuwen;
            maxsx=shuxue;
            maxyy=yingyu;
        }
    }
    cout<<maxname<<' '<<maxyw<<' '<<maxsx<<' '<<maxyy;
    return 0;
}

by 编码落寞 @ 2023-08-25 09:11:19

@yzh___nb

maxzf初始值设为比0小,防止全是0的数据


by 1q2zb @ 2023-08-25 09:11:23

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,yuwen=0,shuxue=0,yingyu=0,zf=0,maxzf,maxyw,maxsx,maxyy;
    string name,maxname;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>name>>yuwen>>shuxue>>yingyu;
        zf=yuwen+shuxue+yingyu;
        if(i==1)
        {
            maxzf=zf;
            maxname=name;
            maxyw=yuwen;
            maxsx=shuxue;
            maxyy=yingyu;
        }
        if(zf>maxzf)
        {
            maxzf=zf;
            maxname=name;
            maxyw=yuwen;
            maxsx=shuxue;
            maxyy=yingyu;
        }
    }
    cout<<maxname<<' '<<maxyw<<' '<<maxsx<<' '<<maxyy;
    return 0;
}

少了一个1人的特判


by yzh___nb @ 2023-08-25 09:14:39

谢谢,关注你们了


|