user-0 提问: 稀疏矩阵加法问题求解

回复:
user: user-0
content: 我又写了个，结果测试输出是 10 10 5 2 3 8 2 7 97 2 9 20 4 6 32 5 0 35    #include <stdio.h> #include <malloc.h> #define MAXSIZE 10002  typedef struct {     int i;     int j;     int e; }Triple; typedef struct {     int row;     int col;     int cnt;     Triple data[MAXSIZE]; }Matrix;  int main() {     int k, n1, n2, n3, sum;     Matrix *A, *B, *C;     A = (Matrix *)malloc(sizeof(Matrix));     B = (Matrix *)malloc(sizeof(Matrix));     C = (Matrix *)malloc(sizeof(Matrix));      scanf("%d%d%d", &A->row, &A->col, &A->cnt);     for(k = 1; k <= A->cnt; k++)         scanf("%d%d%d", &A->data[k].i, &A->data[k].j, &A->data[k].e);     scanf("%d%d%d", &B->row, &B->col, &B->cnt);     for(k = 1; k <= B->cnt; k++)         scanf("%d%d%d", &B->data[k].i, &B->data[k].j, &B->data[k].e);      C->row = A->row;     C->col = A->col;     C->cnt = 0;     n1 = n2 = n3 = 1;      while(n1 <= A->cnt && n2 <= B->cnt)     {         if(A->data[n1].i == B->data[n2].i)         {             if(A->data[n1].j == B->data[n2].j)             {                 sum = A->data[n1].e + B->data[n2].e;                 if(sum)                 {                     C->data[n3].i = A->data[n1].i;                     C->data[n3].j = A->data[n1].j;                     C->data[n3].e = sum;                     n3++;                 }                 n1++;                 n2++;             }             else if(A->data[n1].j > B->data[n2].j)             {                 C->data[n3].i = B->data[n2].i;                 C->data[n3].j = B->data[n2].j;                 C->data[n3].e = B->data[n2].e;                 n2++;                 n3++;             }             else             {                 C->data[n3].i = A->data[n1].i;                 C->data[n3].j = A->data[n1].j;                 C->data[n3].e = A->data[n1].e;                 n1++;                 n3++;             }         }         if(A->data[n1].i < B->data[n2].i)         {             C->data[n3].i = A->data[n1].i;             C->data[n3].j = A->data[n1].j;             C->data[n3].e = A->data[n1].e;             n1++;             n3++;         }         else         {             C->data[n3].i = B->data[n2].i;             C->data[n3].j = B->data[n2].j;             C->data[n3].e = B->data[n2].e;             n2++;             n3++;         }     }      C->cnt = n3-1;     printf("%d %d %d\n", A->row, A->col, C->cnt);     for(k = 1; k <= C->cnt; k++)         printf("%d %d %d\n", C->data[k].i, C->data[k].j, C->data[k].e);      return 0; }

user: user-0
content: #include <stdio.h> #include <malloc.h> #define MAXSIZE 10002  typedef struct {     int i;     int j;     int e; }Triple; typedef struct {     int row;     int col;     int cnt;     Triple data[MAXSIZE]; }Matrix;  int main() {     int k, n1, n2, n3, sum;     Matrix *A, *B, *C;     A = (Matrix *)malloc(sizeof(Matrix));     B = (Matrix *)malloc(sizeof(Matrix));     C = (Matrix *)malloc(sizeof(Matrix));      scanf("%d%d%d", &A->row, &A->col, &A->cnt);     for(k = 1; k <= A->cnt; k++)         scanf("%d%d%d", &A->data[k].i, &A->data[k].j, &A->data[k].e);     scanf("%d%d%d", &B->row, &B->col, &B->cnt);     for(k = 1; k <= B->cnt; k++)         scanf("%d%d%d", &B->data[k].i, &B->data[k].j, &B->data[k].e);      C->row = A->row;     C->col = A->col;     C->cnt = 0;     n1 = n2 = n3 = 1;      //while(n1 <= A->cnt && n2 <= B->cnt)     for(k = 1; k <= A->row; k++)     {         while(A->data[n1].i < k)             n1++;         while(B->data[n2].i < k)             n2++;          while(A->data[n1].i == k && B->data[n2].i == k)         {             if(A->data[n1].j == B->data[n2].j)             {                 sum = A->data[n1].e + B->data[n2].e;                 if(sum)                 {                     C->data[n3].i = k;                     C->data[n3].j = A->data[n1].j;                     C->data[n3].e = sum;                     n3++;                 }                 n1++;                 n2++;             }             else if(A->data[n1].j > B->data[n2].j)             {                 C->data[n3].i = k;                 C->data[n3].j = B->data[n2].j;                 C->data[n3].e = B->data[n2].e;                 n2++;                 n3++;             }             else             {                 C->data[n3].i = k;                 C->data[n3].j = A->data[n1].j;                 C->data[n3].e = A->data[n1].e;                 n1++;                 n3++;             }         }         while(A->data[n1].i == k)         {             C->data[n3].i = k;             C->data[n3].j = A->data[n1].j;             C->data[n3].e = A->data[n1].e;             n1++;             n3++;         }         while(B->data[n2].i == k)         {             C->data[n3].i = k;             C->data[n3].j = B->data[n2].j;             C->data[n3].e = B->data[n2].e;             n2++;             n3++;         }     }      C->cnt = n3-1;     printf("%d %d %d\n", A->row, A->col, C->cnt);     for(k = 1; k <= C->cnt; k++)         printf("%d %d %d\n", C->data[k].i, C->data[k].j, C->data[k].e);      return 0; }