哪位大佬能帮忙对比一下两份代码

P1093 [NOIP2007 普及组] 奖学金

Miller2019 @ 2020-03-11 12:52:12

A-100:

#include<iostream>
#include<algorithm>
using namespace std;
struct stu
{
    int num;
    int c,m,e;
    int sum;
}student[500];
bool cmp(stu a,stu b)
{
    if(a.sum>b.sum)
    {
        return 1;
    }
    if(a.sum<b.sum)
    {
        return 0;
    }
    if(a.c>b.c)
    {
        return 1;
    }
    if(a.c<b.c)
    {
        return 0;
    }
    if(a.num>b.num)
    {
        return 0;
    }
    return 1;
}
int main()
{
    int n;
    cin>>n;
    for(int t=1;t<=n;t++)
    {
        student[t].num=t;
        cin>>student[t].c>>student[t].m>>student[t].e;
        student[t].sum=student[t].c+student[t].m+student[t].e;
    }
    sort(student+1,student+n+1,cmp);
    for(int t=1;t<=5;t++)
    {
        cout<<student[t].num<<' '<<student[t].sum<<"\n";
    }
    return 0;
}

B:90

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
int n,t=0;
struct stu
{
    int num;
    int c,m,e;
    int sum;
}student[500];
bool cmp(stu a,stu b)
{
    if(a.sum!=b.sum)
    {
        return a.sum>b.sum;
     }
    if(a.c!=b.c)
    {
        return a.c>b.c;
     }
    if(a.num!=b.num)
    {
        return a.sum>b.sum;
     }
    return -1;
}
int main()
{
    scanf("%d",&n);
    for(;t<n;t++)
    {
        student[t].num=t;
        cin>>student[t].c>>student[t].m>>student[t].e;
        //scanf("%d%d%d",&stu[t].c,&stu[t].m,&stu[t].e);
        student[t].sum=student[t].c+student[t].m+student[t].e;
    }
    sort(student,student+n,cmp);
    for(t=0;t<5;t++)
    {
        cout<<student[t].num+1<<' '<<student[t].sum<<'\n';
        //printf("%d %d\n",student[t].num+1,student[t].sum);
    }
    return 0;
}

还有,为什么B代码用被注释掉的scanf和printf会编译错误?


by Miller2019 @ 2020-03-11 13:07:56

@UserUnknown ?


by 已注销ZdCpmQy3 @ 2020-03-11 13:10:57

@Miller2019 换成代码1的


by fzj2007 @ 2020-03-11 13:27:15

@UserUnknown 他是0开始的,不用加1.加一的意思是下标从1开始。。


by Miller2019 @ 2020-03-11 13:35:19

所以为什么,它第8个点过不去呢

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
int n,t=0;
struct stu
{
    int num;
    int c,m,e;
    int sum;
}student[500];
bool cmp(stu a,stu b)
{
    if(a.sum!=b.sum)
        return a.sum>b.sum;
    if(a.c!=b.c)
        return a.c>b.c;
    if(a.num!=b.num)
        return a.sum>b.sum;
    return -1;
}
int main()
{
    scanf("%d",&n);
    for(;t<n;t++)
    {
        student[t].num=t;
        scanf("%d%d%d",&student[t].c,&student[t].m,&student[t].e);
        student[t].sum=student[t].c+student[t].m+student[t].e;
    }
    sort(student,student+n,cmp);
    for(t=0;t<5;t++)
    {
        printf("%d %d\n",student[t].num+1,student[t].sum);
    }
    return 0;
}

by Miller2019 @ 2020-03-11 13:35:34

@fzj2007


by Miller2019 @ 2020-03-11 13:36:49

https://www.luogu.com.cn/record/31622389#


by 已注销ZdCpmQy3 @ 2020-03-11 13:47:35

@fzj2007 我记得sort两边一定要加1啊,可能我记错了


by Miller2019 @ 2020-03-11 13:57:38

@UserUnknown 不是,要不要加1觉得的是排序开始和结束的位置


by Miller2019 @ 2020-03-11 13:58:45

我是从student[0]开始排的,所以是sort(student,student+n,cmp);


by Miller2019 @ 2020-03-11 13:59:38

可是我为什么是90分呢。。。


上一页 |