C++ 大数排序问题 请问我的代码错在哪里?

题干:

img

我的代码:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<algorithm>

using namespace std;

struct bign{
    int length;
    int d[1001];
    bign(){
        memset(d,0,sizeof(d));
        length=0;
    }
}N[101];

bool cmp(struct bign a,struct bign b){
    if(a.length>b.length){
        return 0;
    }else if(a.length<b.length){
        return 1;
    }else{
        int i;
        for(i=a.length-1;i>=0;i--){
            if(a.d[i]<b.d[i]){
                return 1;
            }else if(a.d[i]>b.d[i]){
                return 0;
            }
        }
    }
}

int main(){
    int n;
    int j;
    while(scanf("%d",&n)!=EOF){
        int i;
        for(i=0;i<n;i++){
            char c[1002];
            scanf("%s",c);
            for(j=strlen(c)-1;j>=0;j--){
                N[i].d[j]=c[strlen(c)-j-1]-'0';
                N[i].length++;
            }

        }
        sort(N,N+n,cmp);
        for(j=0;j<n;j++){
            for(i=N[j].length-1;i>=0;i--){
                printf("%d",N[j].d[i]);
            }
            printf("\n");
        }
    }
    return 0;
}


OJ网站给我的报错是
img
请问我的代码错在哪里?

在第40行和41行之间,这么改下试试:

for(i=0;i<n;i++){
            N[i].length = 0;
            char c[1002];

问题出在N[101];上,输入的时候没有初始化,上次输入的数据还保留在N里,导致错误。
改成动态数组了,根据输入创建N,你看看。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<algorithm>

using namespace std;

typedef struct _bign
{
    int length;
    int d[1001];
    _bign()
    {
        memset(d, 0, sizeof(d));
        length = 0;
    }
} bign;

bign *N;

bool cmp(bign a, bign b)
{
    if(a.length > b.length)
    {
        return 0;
    }

    if(a.length < b.length)
    {
        return 1;
    }

    for(int i = 0; i < a.length; i++)
    {
        if(a.d[i] < b.d[i])
        {
            return 1;
        }
        else if(a.d[i] > b.d[i])
        {
            return 0;
        }
    }

    return 0;
}


int main()
{
    int n, j, i;

    while(scanf("%d", &n) != EOF)
    {
        if(n < 1 || n > 100)
        {
            break;
        }

        N = (bign *)calloc(n, sizeof(bign));

        for(i = 0; i < n; i++)
        {
            char c[1002];
            scanf("%s", c);

            for(j = 0; j < strlen(c); j++)
            {
                N[i].d[N[i].length] =  c[j] - '0';
                N[i].length++;
            }
        }

        sort(N, N + n, cmp);

        for(j = 0; j < n; j++)
        {
            for(i = 0; i < N[j].length; i++)
            {
                printf("%d", N[j].d[i]);
            }

            printf("\n");
        }

        free(N);
    }

    system("pause");
    return 0;
}