就第一个点WA,没找出问题,哪位大佬来给看看,谢谢

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

cancan1030 @ 2023-06-11 14:35:23

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

by _Haoomff_ @ 2023-06-11 14:52:28

@cancan1030 max需要赋初始值


by cancan1030 @ 2023-06-11 14:58:24

@Haoomff 这样吗?还是不行啊

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

by _Haoomff_ @ 2023-06-11 14:58:43

@cancan1030 赋值-99


by _Haoomff_ @ 2023-06-11 15:01:09

@cancan1030 其实max和all都不用这样用的


by cancan1030 @ 2023-06-11 15:01:12

@Haoomff 成了,感谢


by cancan1030 @ 2023-06-11 15:01:48

@Haoomff 哦?


by _Haoomff_ @ 2023-06-11 15:03:55

max初值赋为-99,all替换为一个计算总分的变量。每次计算出总分后,跟max比较一下,若大于max,max=现在的总分,并把现在的位置记录下来

for(int i=0;i<n;i++){
        cin>>a[i].name>>a[i].c>>a[i].m>>a[i].e;
        cnt=cnt+a[i].c+a[i].m+a[i].e;
        if(cnt>Max){
            Max=cnt;
            j=i;
        }
    }

(这份代码前年打的,丑且变量乱用,懂就行,勿喷)


by _Haoomff_ @ 2023-06-11 15:04:05

@cancan1030


by _Haoomff_ @ 2023-06-11 15:04:55

不好意思,少了一个cnt=0没复制进……


by cancan1030 @ 2023-06-11 15:07:24

@Haoomff 大佬受我一拜


| 下一页