求助,无输出

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

ResonCaolol @ 2023-05-21 13:49:06

#include<bits/stdc++.h>
using namespace std;
int sum[1001];
int maxx;
struct student{
    string name;
    int eng;
    int chi;
    int mat;
}q[1001]; 
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>q[i].name>>q[i].chi>>q[i].mat>>q[i].eng;
        sum[i]=q[i].chi+q[i].eng+q[i].mat;
        max(maxx,sum[i]);
    }
    for(int i=0;i>n;i++)
    {
        if(sum[i]==maxx) cout<<q[i].name<<q[i].chi<<q[i].mat<<q[i].eng;
    }
    return 0;
}

by ZZQF5677 @ 2023-05-21 13:50:05

for(int i=0;i>n;i++)
    {
        if(sum[i]==maxx) cout<<q[i].name<<q[i].chi<<q[i].mat<<q[i].eng;
    }

改为

for(int i=0;i<n;i++)
    {
        if(sum[i]==maxx) cout<<q[i].name<<q[i].chi<<q[i].mat<<q[i].eng;
    }

by ZZQF5677 @ 2023-05-21 13:50:20

@1125__qwq


by 2huk @ 2023-05-21 13:50:21

for(int i=0;i>n;i++)

应改成

for(int i=0;i<n;i++)


by ResonCaolol @ 2023-05-21 13:54:48

改了,还是无输出

#include<bits/stdc++.h>
using namespace std;
int sum[1001];
int maxx;
struct student{
    string name;
    int eng;
    int chi;
    int mat;
}q[1001]; 
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>q[i].name>>q[i].chi>>q[i].mat>>q[i].eng;
        sum[i]=q[i].chi+q[i].eng+q[i].mat;
        max(maxx,sum[i]);
    }
    for(int i=0;i<n;i++)
    {
        if(sum[i]==maxx) cout<<q[i].name<<q[i].chi<<q[i].mat<<q[i].eng;
    }
    return 0;
}

by mcr123456 @ 2023-05-21 14:22:37

在代码中,max(maxx,sum[i])应该改为maxx=max(maxx,sum[i]),否则maxx的值不会更新为最大值。同时,在输出学生信息时,应该添加空格或换行符,否则输出的信息会连在一起。

修改后的代码如下:

#include<点个关注吧>
using namespace std;
int sum[关注吧];
int maxx;
struct student{
    string name;
    int eng;
    int chi;
    int mat;
}q[1001]; 
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>q[i].name>>q[i].chi>>q[i].mat>>q[i].eng;
        sum[i]=q[i].chi+q[i].eng+q[i].mat;
        maxx=max(maxx,sum[i]);
    }
    for(int i=0;i<n;i++)
    {
        if(sum[i]==maxx) cout<<q[i].name<<" "<<q[i].chi<<" "<<q[i].mat<<" "<<q[i].eng<<endl;
    }
    return 0;
}

by ZZQF5677 @ 2023-05-21 14:24:09

@1125__qwq

ok了。

#include<bits/stdc++.h>
using namespace std;
int sum[1001];
int maxx;
struct student{
    string name;
    int eng;
    int chi;
    int mat;
}q[1001]; 
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>q[i].name>>q[i].chi>>q[i].mat>>q[i].eng;
        sum[i]=q[i].chi+q[i].eng+q[i].mat;
        maxx=max(maxx,sum[i]);
    }
    for(int i=0;i<n;i++)
    {
        if(sum[i]==maxx) cout<<q[i].name<< " " << q[i].chi<< " "<<q[i].mat<< " "<<q[i].eng<<"\n";
    }
    return 0;
}

|