50分代码,蒟蒻求助!!

P1093 [NOIP2007 普及组] 奖学金

zmy060301 @ 2021-10-30 10:18:49

如题

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
    int xh,yw,ss,yy,zf;
}a[1001];
bool cmp(node a,node b){
    return a.zf>b.zf;
    if(a.zf==b.zf) return a.yw>b.yw;
    if(a.zf==b.zf&&a.yw>b.yw) return a.xh<b.xh;
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].yw>>a[i].ss>>a[i].yy;
        a[i].xh=i;
        a[i].zf=a[i].yw+a[i].ss+a[i].yy;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=5;i++){
        cout<<a[i].xh<<" "<<a[i].zf<<endl;
    }
    cout<<endl;
    return 0;
}

by xixike @ 2021-10-30 10:55:00

@zmy060301 你的 cmp 有问题,改成这样就可以过了。

bool cmp(node a,node b){
    if(a.zf == b.zf && a.yw == b.yw) return a.xh < b.xh;
    if(a.zf == b.zf) return a.yw > b.yw;
    return a.zf > b.zf;
}

by zmy060301 @ 2021-10-30 11:14:27

@xixike 谢谢大佬


by yingjingxu_NaS2O3 @ 2021-11-15 11:03:03

来了

#include<bits/stdc++.h>
using namespace std;
int dou=0,n;
struct STU
{
    int num;
    int chi;
    int mat;
    int eng;
    int sum;
    void inps()
    {
        num=++dou;
        scanf("%d%d%d",&this->chi,&this->mat,&this->eng);
        this->sum=chi+mat+eng;
    }
    inline outps();
};
STU a[311];
void STU_outps()
{
    for(int i=1;i<=5;i++)
        printf("%d %d\n",a[i].num,a[i].sum);
}
bool STU_cmp(STU x,STU y)
{
    if(x.sum!=y.sum) return x.sum>y.sum;
    if(x.chi!=y.chi) return x.chi>y.chi;
    return x.num<y.num;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        a[i].inps();
    sort(a+1,a+1+n,STU_cmp);
    STU_outps();
    return 0;
}

用C++11提交


|