90分求助

P1093 [NOIP2007 普及组] 奖学金

123uuu @ 2024-02-06 18:55:29

#include<bits/stdc++.h>
using namespace std;
struct Node{
    int zong_fen = 0, yu_wen, shu_xue, ying_yu;
    int xue_hao;
};
bool cmp(Node x, Node y){
    if(x.zong_fen != y.zong_fen) return x.zong_fen > y.zong_fen;
    else if(x.shu_xue != y.shu_xue) return x.shu_xue > y.shu_xue;
    else return x.xue_hao > y.xue_hao;
} 
Node a[10000];
int main(){
    int i, n;
    cin >> n;
    for(i = 1; i <= n; ++i){
        cin >> a[i].shu_xue >> a[i].yu_wen >> a[i].ying_yu;
        a[i].zong_fen = a[i].shu_xue + a[i].yu_wen + a[i].ying_yu;
        a[i].xue_hao = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for(i = 1; i <= 5; ++i){
        cout << a[i].xue_hao << " " << a[i].zong_fen << endl;
    }
return 0;
}

by ye_hao_lun @ 2024-02-06 19:11:48

#include<bits/stdc++.h> 
using namespace std;
struct node
{
    int xue;
    int yu;
    int shu;
    int ying;
    int zong;
}a[1000];
bool cmp(node x,node y)
{
    if(x.zong!=y.zong) return x.zong>y.zong;
    else if(x.yu!=y.yu) return x.yu>y.yu;
    else return x.xue<y.xue;
}
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].xue=i;
        a[i].zong=a[i].yu+a[i].shu+a[i].ying;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=5;i++)
    {
        cout<<a[i].xue<<" "<<a[i].zong<<endl;
    }
    return 0;
}

by gotocspandbetter @ 2024-02-06 19:12:03

else return x.xue_hao > y.xue_hao;

改成

else return x.xue_hao < y.xue_hao;

就行了


by I_Love_DS @ 2024-02-06 19:12:14

那么规定学号小的同学排在前面。

你仔细读读题吧。


by gotocspandbetter @ 2024-02-06 19:12:16

@123uuu


by 123uuu @ 2024-02-07 12:50:43

@gotocspandbetter 感谢dalao


by W_R_Q @ 2024-07-20 09:56:26

#include<bits/stdc++.h>
using namespace std;
struct student{
    int xuehao;
    int zongfen;
    int yuwen;
    int shuxue;
    int yingyu;
}a[310];
int n;
int cmp(student one,student two){
    if(one.zongfen==two.zongfen){
        if(one.yuwen==two.yuwen){
            return one.xuehao<two.xuehao;
        }
        return one.yuwen>two.yuwen;
    }
    return one.zongfen>two.zongfen;
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i].yuwen>>a[i].shuxue>>a[i].yingyu;
        a[i].zongfen=a[i].yuwen+a[i].shuxue+a[i].yingyu;
        a[i].xuehao=i+1;
    }
    sort(a,a+n,cmp); 
    for(int i=0;i<5;i++){
        cout<<a[i].xuehao<<" "<<a[i].zongfen<<endl;
    }
    return 0; 
}

|