求助,只有80分

P1093 [NOIP2007 普及组] 奖学金

Cake_AL @ 2019-09-27 12:56:37

#include<bits/stdc++.h>
using namespace std;
int n;
struct node {
    int c,m,e,num,z;
}a[1000];
inline int read(){
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-48;c=getchar();}
    return x*f;
}
int cmp(node x,node y){
    if(x.z==y.z){
        if(x.c>y.c) return x.c>y.c;
        else return x.num<y.num;
    }
    else return x.z>y.z;
}
int main(){
    n=read();
    for(int i=1;i<=n;i++){
        a[i].num=i;
        a[i].c=read();
        a[i].m=read();
        a[i].e=read();
        a[i].z=a[i].c+a[i].m+a[i].e;
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=5;i++){
        printf("%d %d",a[i].num,a[i].z);
        if(i<=4) printf("\n");
    }
    return 0;
}

6和8错,6点我输出:

66 276

88 270

19 270

101 270

93 267

答案是:

66 276

88 270

19 270

101 270

93 267


by 安子 @ 2019-09-27 13:13:58

@Cake_AL

if(x.c>y.c) return x.c>y.c;
        else return x.num<y.num;

这一段写错了

应该是:

int cmp(node x,node y){
    if(x.z==y.z){
        if(x.c==y.c) return x.num<y.num;
        else return x.c>y.c;
    }
    else return x.z>y.z;
}

by Cake_AL @ 2019-09-28 21:44:15

@安子 懂了,感谢


|