以三元组表为存储结构实现矩阵相加(耿5.7)
Time Limit: 3000ms, Memory Limit: 10000KB , Accepted: 247, Total Submissions: 1092
Description
假设稀疏矩阵A和B均以三元组表作为存储结构。试编写矩阵相加的程序,另设三元组表C存放结果矩阵。矩阵大小为m行n列(0<m,n<100)
Input
第一行输入t1,t2(0<t1,t2<100) ,t1和t2分别是矩阵A和B中非零元素的个数,后面t1+t2行分别输入A和B中的元素,用三元组表示。
Output
输出三元组表C。
-
Sample Input
3 3 1 2 3 3 2 1 3 4 2 1 1 4 3 2 5 3 4 1
-
Sample Output
1 1 4 1 2 3 3 2 6 3 4 3
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int row;
int col;
int num;
}node;
typedef struct{
int cnt1,cnt2;
node matrix1[101];
node matrix2[101];
}juzhen;
void init(juzhen *p){
int t1,t2,i=0;
int x,y,z,tmp;
scanf("%d%d",&t1,&t2);
p->cnt1=t1;
p->cnt2=t2;
while(1){
scanf("%d%d%d",&x,&y,&z);
p->matrix1[i].row = x;
p->matrix1[i].col = y;
p->matrix1[i]