求助,第一个点WA

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

czk111 @ 2023-01-08 20:57:10

#include<bits/stdc++.h>
using namespace std;
struct node{
    string name;
    int yu,shu,yin;
    int sum;
}a[1010];
int n;
bool cmp(node a,node b){
    if(a.sum>b.sum){
        return true;
    }
    return false;
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i].name>>a[i].yu>>a[i].shu>>a[i].yin;
        a[i].sum=a[i].yu+a[i].shu+a[i].yin;
    }
    sort(a,a+n,cmp);
    cout<<a[0].name<<" "<<a[0].yu<<" "<<a[0].shu<<" "<<a[0].yin;
} 

by InversionShadow @ 2023-01-08 20:59:06

@czk111 题目说了如果有多个总分相同的学生,输出靠前的那位。


by czk111 @ 2023-01-08 21:00:28

所以如何解决,给每个学生一个编号?


by InversionShadow @ 2023-01-08 21:02:42

@czk111 用 stable_sort()


by mooktian @ 2023-01-20 15:15:13

@czk111 第一个点,最高的总分是0,真坑。


by wlx219 @ 2023-01-24 14:56:25

@mooktian 感谢,这离谱的测试点


by b4d_aoo1e @ 2023-03-05 17:20:16

@yuandingquan1101 我也是这个问题,用stable_sort()就过了hh


by 1ooper @ 2023-05-03 23:14:43

在struct里写一个int x;

重点:

cmp:

if(a.sum==b.sum)return a.x<b.x; return a.sum>b.sum;

重点2

for(int i=0;i<n;i++){                cin>>a[i].name>>a[i].yu>>a[i].shu>>a[i].yin;
  a[i].sum=a[i].yu+a[i].shu+a[i].yin;
  a[i].x=i;//重点
}

by 1ooper @ 2023-05-03 23:16:39

或:

#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct node{
    long long x,w,s,y,ans;
    string na;
}k[N];
bool cmp(node a,node z){
    return a.ans>z.ans;
}
int main(){
    long long n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>k[i].na>>k[i].w>>k[i].s>>k[i].y;
        k[i].ans=k[i].w+k[i].s+k[i].y;
        k[i].x=i;
    }
    stable_sort(k+1,k+1+n,cmp);
    cout<<k[1].na<<" "<<k[1].w<<" "<<k[1].s<<" "<<k[1].y;
}

|