Sum of sparse matrices X11263


Statement
 

pdf   zip

A matrix is said to be sparse when almost all its values are zero. Write a program that computes the sum of sparse matrices.

Input

Input is a sequence of cases. Each case consits on the description of two sparse matrix. An sparse matrix is described by a non negative integer number nn followed by a sequence of nn integer triples. Triple ijvi\; j\; v with v0v \not= 0 defines vv as the value of the matrix at row ii column jj. We assume that ii and jj are non negative and that matrix values at non listed positions are zero. For instance, the first output matrix in the sample output below is: (4000000000520001)\left( \begin{array}{cccc} 4 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 5 & 2 \\ 0 & 0 & 0 & 1 \end{array} \right)

Output

For each case, write the sum of the two input matrices as a sequence of triples. Write only those triples that show a non zero value of the sum matrix. Write one triple at each line. Triples must appear ordered by rows. Triples of the same row must appear ordered by columns. After each case, print a line with ten dashes.

Public test cases
  • Input

    3
    1 1 1
    0 0 5
    2 3 1
    5
    3 3 1
    0 0 -1
    2 2 5
    1 1 -1
    2 3 1
    
    1
    0 0 5
    1 
    0 0 -5
    
    3
    2 1 -1
    0 0 1
    1 2 1
    2
    0 0 -2
    2 1  1
    

    Output

    0 0 4
    2 2 5
    2 3 2
    3 3 1
    ----------
    ----------
    0 0 -1
    1 2 1
    ----------
    
  • Information
    Author
    Jorge Castro
    Language
    English
    Translator
    Original language
    Catalan
    Other languages
    Catalan Spanish
    Official solutions
    Python
    User solutions
    Python