90分求助

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

dl_yyds @ 2022-12-26 11:31:47

rt,第一个点WA了求助

#include<iostream>
#include<string>
using namespace std;

struct student
{
    string name;
    int ch,ma,en;
}a,ans;

int main()
{
    int n;
    cin>>n;
    if(n==1)
    {
        cin>>a.name>>a.ch>>a.ma>>a.en;
        cout<<a.name<<" "<<a.ch<<" "<<a.ma<<" "<<a.en;
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>a.name>>a.ch>>a.ma>>a.en;
        if(a.ch+a.ma+a.en>ans.ch+ans.ma+ans.en)
        {
            ans.name=a.name;
            ans.ch=a.ch;
            ans.ma=a.ma;
            ans.en=a.en;
        }
    }
    cout<<ans.name<<" "<<ans.ch<<" "<<ans.ma<<" "<<ans.en;
    return 0;
}

by Jerrlee✅ @ 2022-12-26 11:42:45

@dl_yyds

#include<iostream>
#include<string>
using namespace std;

struct student
{
    string name;
    int ch,ma,en;
}a,ans;

int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a.name>>a.ch>>a.ma>>a.en;
        if(i==1){
            ans.name=a.name;
            ans.ch=a.ch;
            ans.ma=a.ma;
            ans.en=a.en;
            continue;
        }
        if(a.ch+a.ma+a.en>ans.ch+ans.ma+ans.en)
        {
            ans.name=a.name;
            ans.ch=a.ch;
            ans.ma=a.ma;
            ans.en=a.en;
        }
    }
    cout<<ans.name<<" "<<ans.ch<<" "<<ans.ma<<" "<<ans.en;
}

给你改了一下


by Jerrlee✅ @ 2022-12-26 11:45:33

一开始要把 ans 赋为第一个学生,不然可能会出现一些奇怪的错误(毕竟 ans 为全局变量,初值是 0,答案也全是 0 时你的程序就只因了

hack:

in:

2
a 0 0 0
b 0 0 0

out:

 0 0 0

ans:

a 0 0 0

by dl_yyds @ 2022-12-26 11:46:23

@Jerrlee✅ 谢谢


by chenshixue @ 2023-02-11 12:06:23

你好


|