90分,求助

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

x1007 @ 2023-11-30 18:14:40

#include<bits/stdc++.h>
#include<iostream>
#include <string>
using namespace std;
struct student{
    string name="0";
    int chinese,math,english;
}a,ans;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a.name>>a.chinese>>a.math>>a.english;
        if(a.chinese+a.math+a.english>ans.chinese+ans.math+ans.english)
            ans=a;
    }
    cout<<ans.name<<" "<<ans.chinese<<" "<<ans.math<<" "<<ans.english<<endl;
    return 0;
}

by penguin_is_cool @ 2023-11-30 18:27:33

@x1007 呵呵,看不懂.

你看看我的代码:

#include <iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    string a[n];
    int b[n],c[n],d[n],sum[n],e;
    for (int i=0;i<n;i++){
        cin>>a[i]>>b[i]>>c[i]>>d[i];
    }
    for (int i=0;i<n;i++){
        sum[i]=b[i]+c[i]+d[i];
    }
    e=sum[0];
    for (int i=0;i<n;i++){
        if (e<sum[i])e=sum[i];
    }
    for (int i=0;i<n;i++){
        if (sum[i]==e){
            cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<" "<<d[i];
            break;
        }
    }
}

你把循环的if判断里面在ans=a;后面加break跳出循环试试


by x1007 @ 2023-11-30 18:30:07

@penguin_is_cool 错的更离谱了,连输出的输出不了


by Vector_Ding @ 2023-11-30 18:34:03

那题啊?


by x1007 @ 2023-11-30 18:37:10

@dingzy p5740


by 0907_WDS_0731 @ 2023-11-30 19:09:48

如果有多个总分相同的学生,输出靠前的那位。

是不是因为这个


by penguin_is_cool @ 2023-11-30 19:53:17

@x1007 那我就没办法了,因为我才刚学没多久,你的格式看不懂


by Vector_Ding @ 2023-11-30 19:56:40

#include<cstdio>
#include<iostream>
using namespace std;
string name[1005];
int cg[1005],mg[1005],eg[1005];
int main(){
    int n,max=-999999,t;
    scanf("%d",&n);
    for(int a=0;a<n;a++)cin>>name[a]>>cg[a]>>mg[a]>>eg[a];
    for(int b=0;b<n;b++){
        if(cg[b]+mg[b]+eg[b]>max){
            max=cg[b]+mg[b]+eg[b];成立更
            t=b;
        }
    }
    cout<<name[t]<<" "<<cg[t]<<" "<<mg[t]<<" "<<eg[t];
    return 0;
}

by Vector_Ding @ 2023-11-30 19:57:42

这就好了 AC了


|