sort RE!

P1093 [NOIP2007 普及组] 奖学金

Magallan_forever @ 2018-12-15 14:24:09

sort垃圾

#include<bits/stdc++.h>
using namespace std;
struct score{
    int ID,Chinese,Math,English;
};
score student[301];
bool cmp(score a,score b){
    if(a.Chinese+a.Math+a.English>b.Chinese+b.Math+b.English){
        return true;
    }
    if(a.Chinese+a.Math+a.English==b.Chinese+b.Math+b.English){
        if(a.Chinese>b.Chinese){
            return true;
        }
        if(a.Chinese==b.Chinese){
            return a.ID<b.ID;
        }
        else{
            return false;
        }
    }
    else{
        return false;
    }
}
int main(){
    int i0,n;
    scanf("%d",&n);
    for(i0=0;i0<n;i0++){
        scanf("%d%d%d",&student[i0].Chinese,&student[i0].Math,&student[i0].English);
        student[i0].ID=i0+1;
    }
    sort(student,student+4*n-1,cmp);
    for(i0=0;i0<5;i0++){
        printf("%d %d\n",student[i0].ID,student[i0].Chinese+student[i0].Math+student[i0].English);
    }
    return 0;
}

by Magallan_forever @ 2018-12-15 14:41:09

#include<iostream>
#include<algorithm>
using namespace std;
struct stu
{
    int num;//编号
    int c,m,e; 
    int sum;
}student[310];
bool cmp(stu a,stu b)
{
    if(a.sum>b.sum) return 1;
    else if(a.sum<b.sum) return 0;
    else
    {
        if(a.c>b.c) return 1;
        else if(a.c<b.c) return 0;
        else
        {
            if(a.num>b.num) return 0;
            else return 1;
        }
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        student[i].num=i;//录入编号
        cin>>student[i].c>>student[i].m>>student[i].e;//输入
        student[i].sum=student[i].c+student[i].m+student[i].e;//计算总分
    }
    sort(student+1,student+1+n,cmp);
    for(int i=1;i<=5;i++)
        cout<<student[i].num<<' '<<student[i].sum<<endl;
    return 0;
}

其他人用sortAC的!!!!!!


by Victorique_De_Blois @ 2018-12-15 14:41:17

开多一百个又不会怎么样,又不是卡得很死的那种题。


by Magallan_forever @ 2018-12-15 14:47:48

经过比对,结论:垃圾sort

别人的sort
sort(student+1,student+1+n,cmp);
我的sort
sort(student+1,student+4*n-1,cmp);

为什么要加1加n???


by Magallan_forever @ 2018-12-15 14:49:43

我的sort

sort(student,student+4*n-1,cmp);

by tgs9311 @ 2019-01-23 16:53:00

@qbu666666 大哥你这sort在干什么……神O(既然你开了结构体,那么它算一个整体啊…………)


by Magallan_forever @ 2019-01-23 20:09:07

@shiguotao1139那是我还很弱的时候……


上一页 |