Sum of sparse matrices

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 n followed by a sequence of n integer triples. Triple
i j v with v ≠ 0 defines v as the value of the matrix at row i column j.
We assume that i and j 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:
$$\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.

Problem information

Author: Jorge Castro

Generation: 2026-01-25T13:30:43.203Z

© Jutge.org, 2006–2026.
https://jutge.org
