最后两组数据运行时会多出三个奇怪的数据,求解答!!

P1093 [NOIP2007 普及组] 奖学金

XDY18LIUDENGLIN @ 2019-12-08 13:20:10

#include"stdio.h"
#include"stdlib.h"
struct student
{
    int ID;
    int Chinese;
    int Math;
    int Egilsh;
    int sum;
};

int cmp(const void *a,const void *b)
{
    struct student *c=(struct student*)a;
    struct student *d=(struct student*)b;
    if(c->sum!=d->sum)
        return d->sum-c->sum;
    else
    {
        if(c->Chinese!=d->Chinese)
            return d->Chinese-c->Chinese;
        else
            return c->ID-d->ID;
    }
}

int main()
{
    int N,i;
    while(~scanf("%d",&N))
    {
        struct student *nums=(struct student*)malloc(sizeof(struct student)*N);
        for(i=0; i<N; i++)
        {
            scanf("%d %d %d",&nums[i].Chinese,&nums[i].Math,&nums[i].Egilsh);
            nums[i].ID=i+1;
            nums[i].sum=nums[i].Math+nums[i].Chinese+nums[i].Egilsh;
        }
        qsort(nums,N,sizeof(nums[0]),cmp);
        for(i=0; i<=4; i++)
            printf("%d %d\n",nums[i].ID,nums[i].sum);
        printf("==||=\n");
    }
    printf("|||||||\n");
    return 0;
}

by XDY18LIUDENGLIN @ 2019-12-08 13:34:09

多行输入就是错的,单行输入就AC了,什么情况,有人知道吗?


by Magallan_forever @ 2019-12-08 13:49:03

首先,把倒数第3,倒数第5行的代码删掉

然后,这是我的:

#include<bits/stdc++.h>
using namespace std;
struct score{
    int ID,Chinese,Math,English;
};
score student[310];
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+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;
}

但是您不是已经A了吗


by XDY18LIUDENGLIN @ 2019-12-09 00:51:12

@AT是女孩子 我们的思路是一样的,但是我用多行输入就不行(while(scanf("%d",&N)!=EOF))就会出错,不加就AC了,我有点不懂为什么?


by XDY18LIUDENGLIN @ 2019-12-09 00:53:56

@XDY18LIUDENGLIN 那后面几个输出是我加上检测哪里会多输出的,第8个测试案例会多出一行数据,用多行输入的话


by 今年必上厦大 @ 2020-02-08 10:15:54

@XDY18LIUDENGLIN 我打开倒数第二个输入文件看了,他n=220,可是接下来跟着有221行。。。这数据有问题吧


by Huangzzzzz @ 2020-03-05 10:36:29

这个第八组和第九组应该是一次输入之后还有多组无用数据,我比对了,第八组最后会多一个无用的97 69 56


|