# 90分 , 第一个点过不去 ! ! ! 求助 , 感谢!

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

Xmd123456 @ 2023-04-08 23:36:43


#include<iostream>
#include<string>
using namespace std;
struct student {
    string name;
    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 __wjy__ @ 2023-04-09 18:20:26

缺了一个判断id,再细读一下题目“如果有多个总分相同的学生,输出靠前的那位。” 再结构体再加,还要再sort里加cmp函数


by __wjy__ @ 2023-04-09 18:23:15

#include<bits/stdc++.h>
using namespace std;
struct stu{
    string name;
    int sx,yw,yy,zf,id;
}a[1010];
bool cmp(stu x,stu y)
{
    if(x.zf==y.zf)return x.id<y.id;
    return x.zf>y.zf;
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].name>>a[i].sx>>a[i].yw>>a[i].yy;
        a[i].zf=a[i].sx+a[i].yy+a[i].yw;
        a[i].id=i;
    }
    sort(a+1,a+n+1,cmp);
    cout<<a[1].name<<" "<<a[1].sx<<" "<<a[1].yw<<" "<<a[1].yy;
    return 0;
}

by Xmd123456 @ 2023-04-22 13:09:19

@wujunyu2011 感谢


|