满江红

P1093 [NOIP2007 普及组] 奖学金

xjj110626 @ 2023-12-24 17:18:49

995

#include<bits/stdc++.h>
using namespace std;
struct student{
    int n,c,m,e,s;
}q[310];
int main()
{

    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>q[i].c>>q[i].m>>q[i].e;
        q[i].n=i;
        q[i].s=q[i].c+q[i].m+q[i].e;
    }
    for(int i=0;i<n;i++)
        for(int j=1;j<=n-i;j++)
            if(q[j].s<q[j+1].s||(q[j].s==q[j+1].s&&q[j].c<q[j+1].c)||(q[j].s==q[j+1].s&&q[j].c==q[j+1].c&&q[j].n>q[j].n))
                swap(q[j],q[j+1]);
    for(int i=0;i<5;i++)
        cout<<q[i].n<<' '<<q[i].s<<endl;
    return 0;
}

by RockyChen @ 2023-12-24 17:51:56

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
struct node{
    int yuwen;
    int sum;
    int id;
    //不定义数学与英语成绩,因为它们不参与到名次的高低问题
}a[100001];
int n,shuxue,yingyu;
int cmp(node x,node y){
    if(x.sum!=y.sum){
        return x.sum>y.sum;//总分大优先
    }
    if(x.yuwen!=y.yuwen){
        return x.yuwen>y.yuwen;//语文大优先
    }
    if(x.id!=y.id){
        return x.id<y.id;//学号小优先
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].yuwen>>shuxue>>yingyu;
        a[i].sum=a[i].yuwen+shuxue+yingyu;//处理总分
        a[i].id=i;//处理学号
    }
    sort(a+1,a+n+1,cmp);
    //输出前5名
    for(int i=1;i<=5;i++){
        cout<<a[i].id<<" "<<a[i].sum<<endl;
    }
    return 0;
}

by xueshengyi @ 2023-12-24 18:03:00

你最后一个循环的范围写错了


by xueshengyi @ 2023-12-24 18:03:29

应该是

i=1;i<=5;i++

by xjj110626 @ 2023-12-27 15:37:47

@xueshengyi 感谢大佬! 关注已OK!


|