90分求助,第一个测试点WA

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

jyz120609 @ 2022-08-29 11:52:20

#include <bits/stdc++.h>
using namespace std;
struct node {
    string name;
    int c,m,e,z;
} a[1001];
bool cmp(node a,node b) {
    if(a.z==b.z) return a.name>b.name;
    else return a.z>b.z;
}
int main() {
    int n;
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i].name>>a[i].c>>a[i].m>>a[i].e;
        a[i].z=a[i].c+a[i].m+a[i].e;
    }
    sort(a+1,a+n+1,cmp);
    cout<<a[1].name<<" "<<a[1].c<<" "<<a[1].m<<" "<<a[1].e;
}

by _Hu_Tao @ 2022-08-29 11:56:43

@jyz120609 给你我的AC代码

#include<bits/stdc++.h>
using namespace std;
struct student{
    string name;
    int d,b,c;
}a[1005];
int main()
{
    int n,maxx=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].name>>a[i].d>>a[i].b>>a[i].c;
        maxx=max(maxx,a[i].d+a[i].b+a[i].c);
    }
    for(int i=1;i<=n;i++){
        if(a[i].d+a[i].b+a[i].c==maxx){
            cout<<a[i].name<<" "<<a[i].d<<" "<<a[i].b<<" "<<a[i].c;
            break;
        }
    }
    return 0;
}

by jyz120609 @ 2022-08-29 11:57:50

@_Hu_Tao 谢谢


by Accelessar @ 2022-08-29 11:58:24

@jyz120609 题目中说的是如果总分相同,输出靠前的那位,而不是输出名字字典序小的


by _Hu_Tao @ 2022-08-29 11:58:57

@jyz120609 你这个也可以 但是你仔细看看

return a.name>b.name

你确定字符串能比较?


by _Hu_Tao @ 2022-08-29 12:01:03

@jyz120609 这是改了你的代码AC的

#include <bits/stdc++.h>
using namespace std;
struct node {
    string name;
    int c,m,e,z;
    int num;
} a[1001];
bool cmp(node a,node b) {
    if(a.z==b.z) return a.num<b.num;
    else return a.z>b.z;
}
int main() {
    int n;
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i].name>>a[i].c>>a[i].m>>a[i].e;
        a[i].z=a[i].c+a[i].m+a[i].e;
        a[i].num=i;
    }
    sort(a+1,a+n+1,cmp);
    cout<<a[1].name<<" "<<a[1].c<<" "<<a[1].m<<" "<<a[1].e;
}

by _Hu_Tao @ 2022-08-29 12:02:02

@jyz120609 求个关注


by 欧阳逸owen @ 2022-09-02 20:36:36

@_Hu_Tao 其实你的代码可以简单化:

#include<bits/stdc++.h>
using namespace std;
struct c{
    string name;
    int m,e,c,all;
}b[10000005];
int main(){
    int a,maxx=INT_MIN,m;
    cin >> a;
    for(int i=0;i<a;i++){
        cin >> b[i].name >> b[i].c >> b[i].m >> b[i].e;
        b[i].all=b[i].c+b[i].m+b[i].e;
        if(maxx<b[i].all){
            maxx=b[i].all;
            m=i;
        }
    } 
    cout << b[m].name << " " << b[m].c << " " << b[m].m << " " << b[m].e;
    return 0;
}

|