各位大佬,能不能帮我看看,90分QwQ~~~~~~~

P1093 [NOIP2007 普及组] 奖学金

Studyingson @ 2023-07-25 17:00:49

#include<bits/stdc++.h>
using namespace std;
struct student{
    int xuehao;
    int yu,shu,ying,sum;
}a[305];
bool cmp(student x,student y){
    if(x.sum>y.sum) return 1;
    else
    if(x.sum<y.sum) return 0;
    if(x.sum==y.sum) 
    {
        if(x.yu>y.yu)
        return 1;
        else 
        return 0;
    }
    if(x.sum==y.sum&&x.yu==y.yu)
        {
            if(x.xuehao<y.xuehao)
            return 0;
            else return 1; 
        }
}
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].xuehao=i;
        }
    for(int i=1;i<=n;i++)
        a[i].sum=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].xuehao<<" "<<a[i].sum<<endl;
 } 

by wunaidedanjuan @ 2023-07-25 17:22:21

浅看了一下,你的排序函数有点问题(我有点看不懂QAQ),帮你改了一下代码

#include<bits/stdc++.h>
using namespace std;
struct student{
    int xuehao;
    int yu,shu,ying,sum;
}a[305];
//bool cmp(student x,student y){
//    if(x.sum>y.sum) return 1;
//    else
//    if(x.sum<y.sum) return 0;
//    if(x.sum==y.sum) 
//    {
//        if(x.yu>y.yu)
//        return 1;
//        else 
//        return 0;
//    }
//    if(x.sum==y.sum&&x.yu==y.yu)
//        {
//            if(x.xuehao<y.xuehao)
//            return 0;
//            else return 1; 
//        }
//}
bool cmp(student x,student y)
{
    if(x.sum==y.sum)
    {
        if(x.yu==y.yu)
            return x.xuehao<y.xuehao;
        return x.yu>y.yu;
    }
    return x.sum>y.sum;
}
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].xuehao=i;
        }
    for(int i=1;i<=n;i++)
        a[i].sum=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].xuehao<<" "<<a[i].sum<<endl;
 } 

by __Tonycyt__ @ 2023-07-25 17:26:14

@zll654321 20行 < 改成 >


by __Tonycyt__ @ 2023-07-25 17:28:16

@wunaidedanjuan 他的排序函数确实有点复杂了


by __Tonycyt__ @ 2023-07-25 17:29:31

@zll654321 另外11行有一个错误(虽然不改也能过),要加一个&&x.yu!=y.yu


by __Tonycyt__ @ 2023-07-25 17:30:44

@wunaidedanjuan 他的排序函数看不懂倒不至于,就是马蜂离谱


by Studyingson @ 2023-07-27 13:51:41

谢谢两位大佬(膜拜)~~


|