数据结构30——稀疏矩阵加法,实现C=A+B

Description

输入两个稀疏矩阵,输出它们相加的结果。

Input

第一行输入四个正整数,分别是两个矩阵的行m、列n、第一个矩阵的非零元素的个数t1和第二个矩阵的非零元素的个数t2。
接下来的t1+t2行是三元组,分别是第一个矩阵的数据和第二个矩阵的数据。三元组的第一个元素表示行号,第二个元素表示列号,第三个元素是该项的值。

Output

输出相加后的矩阵三元组。

  • Sample Input 
    3 4 3 2
    1 1 1
    1 3 1
    2 2 2
    1 2 1
    2 2 3
  • Sample Output
    1 1 1
    1 2 1
    1 3 1
    

    2 2 5

  • #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 n,m,t1,t2,i=0;
    	int x,y,z,tmp;
    	scanf("%d%d%d%d",&n,&m,&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].num = z;
            i++;
            if(i==t1) break;
    	}
    	i=0;
    	while(1){
    		scanf("%d%d%d",&x,&y,&z);
    	    p->matrix2[i].row = x;
            p->matrix2[i].col = y;
            p->matrix2[i].num = z;
            i++;
            if(i==t2) break;
    	}
    }
    
    void add(juzhen *p){
        int i,j,k,flag;
    	int tmp;
    	k=p->cnt1-1;
    	for(i=0;i < p->cnt2;i++){
    		flag=1;
    		for(j=0;j < p->cnt1;j++){
    			if(p->matrix1[j].row == p->matrix2[i].row && p->matrix1[j].col == p->matrix2[i].col){
    			    p->matrix1[j].num +=p->matrix2[i].num;
    			    flag=0;
    			    break;
    			}	
    		}
    		if(flag){
    			p->matrix1[++k].row = p->matrix2[i].row;
    			p->matrix1[k].col = p->matrix2[i].col;
    			p->matrix1[k].num = p->matrix2[i].num;
    		}
    	}
    	p->cnt1 = k+1;
    	
    	for(i=0;i < k;i++){
    		for(j=i;j<k+1;j++){
    			if(p->matrix1[i].row > p->matrix1[j].row){  
                    tmp = p->matrix1[i].row;  
                    p->matrix1[i].row = p->matrix1[j].row;  
                    p->matrix1[j].row = tmp;  
                    tmp =p ->matrix1[i].col;  
                    p->matrix1[i].col = p->matrix1[j].col;  
                    p->matrix1[j].col = tmp;  
                    tmp = p->matrix1[i].num;  
                    p->matrix1[i].num = p->matrix1[j].num;  
                    p->matrix1[j].num = tmp;  
                } 
    		}
    	}
    	for(i=0;i<k;i++){  
            for(j=i;j<k+1;j++){  
                if(p->matrix1[i].row == p->matrix1[j].row && p->matrix1[i].col > p->matrix1[j].col){  
                    tmp = p->matrix1[i].col;  
                    p->matrix1[i].col = p->matrix1[j].col;  
                    p->matrix1[j].col = tmp;  
                    tmp = p->matrix1[i].num;  
                    p->matrix1[i].num = p->matrix1[j].num;  
                    p->matrix1[j].num = tmp;  
                }  
            }  
       } 
    }  
    
    void output(juzhen *p){
    	int tmp=0;
    	while((p->cnt1)--){
    		printf("%d %d %d\n",p->matrix1[tmp].row,p->matrix1[tmp].col,p->matrix1[tmp].num);
    		tmp++;
    	}
    }
    
    int main(){
         juzhen *p=(juzhen *)malloc(sizeof(juzhen));
    	 init(p);
    	 add(p);
    	 output(p);
    	 return 0;
    }


猜你喜欢

转载自blog.csdn.net/chengchencheng/article/details/79935436