一个70分,一个50分,求大佬

P1093 [NOIP2007 普及组] 奖学金

HuangWenJun123 @ 2022-07-19 10:55:11

70分

#include <bits/stdc++.h>
using namespace std;
struct student
{
    int xuehao;
    int yu,shu,ying,zf; 
    int ai;
};
bool cmp(student x,student y)
{
    return (x.zf>y.zf||(x.zf==y.zf&&x.ai<y.ai));
}

student a[1002];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].yu>>a[i].shu>>a[i].ying;
        a[i].zf=a[i].yu+a[i].shu+a[i].ying;
        a[i].ai=i;
        a[i].xuehao=i;
    }
    sort(a+1,a+n+1,cmp);
    for(int i = 1;i<=5;i++)
    {   
        cout<<a[i].xuehao<<" "<<a[i].zf<<endl;
    }
    return 0;
 } 

50分

#include <bits/stdc++.h>
using namespace std;
struct student
{
    int xuehao;
    int yu,shu,ying,zf; 
    int ai;
};
bool cmp(student x,student y)
{
    return (x.zf>y.zf);
    if(x.zf==y.zf)
    {
        return (x.yu>y.yu);
        if(x.yu==y.yu)
        {
            return (x.xuehao<y.xuehao);
        }
    }
}

student a[1002];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].yu>>a[i].shu>>a[i].ying;
        a[i].zf=a[i].yu+a[i].shu+a[i].ying;
        a[i].ai=i;
        a[i].xuehao=i;
    }
    sort(a+1,a+n+1,cmp);
    for(int i = 1;i<=5;i++)
    {   
        cout<<a[i].xuehao<<" "<<a[i].zf<<endl;
    }
    return 0;
 } 

求改


by dyyzy @ 2022-07-19 11:51:26

结构体的xuehao和ai是一个东西,建议改成一个

问题主要出在cmp上,这道题比较谁排名靠前有明显的先后顺序,即先比较总分,再比较语文分数,最后才能比较学号,不是并列的关系 像这样

bool cmp(student x,student y)
{
    if(x.zf!=y.zf)
        return x.zf>y.zf;
    else if(x.yu!=y.yu)
        return x.yu>y.yu;
    return x.xuehao<y.xuehao;
}

然后就这样了


by HuangWenJun123 @ 2022-07-19 13:28:17

谢谢大佬!


|