80分过不去求助

P1093 [NOIP2007 普及组] 奖学金

dog_of_mayi_is_me @ 2022-05-20 17:39:43


#include<bits/stdc++.h>
using namespace std;
struct student{
    int H,C,M,E,Z;//学号,语,数,英,总分 
};
student a[305];
int main()
{
    int n,num;
    cin>>n;
    for(int i=1;i<=n;i++)
        {
            cin>>a[i].C;cin>>a[i].M;cin>>a[i].E;
            a[i].Z=a[i].C+a[i].M+a[i].E;
            a[i].H=i;
        }   //输入
    for(int i=1;i<=n;i++)
    {
        //每次循环完结果为将:最大移到最后(即n-i个),其余往前移一个
        for(int j=1;j<=n-i;j++)
        {   
            if(a[j].Z<a[j+1].Z)
            {
                num=a[j+1].C;
                a[j+1].C=a[j].C;
                a[j].C=num;
                num=a[j+1].Z;
                a[j+1].Z=a[j].Z;
                a[j].Z=num;
                num=a[j+1].H;
                a[j+1].H=a[j].H;
                a[j].H=num;
            }
            if(a[j].Z==a[j+1].Z)
            {
                if(a[j].C<a[j+1].C)
                {
                    num=a[j+1].H;
                    a[j+1].H=a[j].H;
                    a[j].H=num;
                }
                if(a[j].C==a[j+1].C)
                {
                    if(a[j].H<a[j+1].H)
                    {
                        num=a[j+1].H;
                        a[j+1].H=a[j].H;
                        a[j].H=num;
                    }

                }
            }
        } 
    }
        //每次循环完结果为将:最大移到最后(即n-i个),其余往前移一个
    for(int i=1;i<=5;i++)
        cout<<a[i].H<<" "<<a[i].Z<<endl;
}

by Fcersoka @ 2022-05-21 09:05:20

#include<bits/stdc++.h>
using namespace std;
struct student{
    int Chinese;
    int math;
    int English;
    int hao;
    int score; 
}a[100001];
bool cmp(student x,student y)
{
    if(x.score==y.score)
    {
        if(x.Chinese==y.Chinese)
        return x.hao<y.hao;
        return x.Chinese>y.Chinese; 
        }
    return x.score>y.score; 
} 
int n,z=1;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].Chinese>>a[i].math>>a[i].English;
        a[i].hao=z++;
        a[i].score=a[i].Chinese+a[i].math+a[i].English;
        }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=5;i++)
    cout<<a[i].hao<<" "<<a[i].score<<endl;
    return 0;
}

by Fcersoka @ 2022-05-21 09:06:01

其实我觉得sort更好用


|