三元组顺序表表示的稀疏矩阵加法。
输入格式:
输入第1行为两个同型矩阵的行数m、列数n,矩阵A的非零元素个数t1,矩阵B的非零元素个数t2。 按行优先顺序依次输入矩阵A三元组数据,共t1行,每行3个数,分别表示非零元素的行标、列标和值。 按行优先顺序依次输入矩阵B三元组数据,共t2行,每行3个数,分别表示非零元素的行标、列标和值。
输出格式:
输出第1行为相加后矩阵行数m、列数n及非零元素个数t。 输出t行相加后的三元组顺序表结果,每行输出非零元素的行标、列标和值,每行数据之间用空格分隔。
输入样例1:
4 4 3 4
0 1 -5
1 3 1
2 2 1
0 1 3
1 3 -1
3 0 5
3 3 7
输出样例1:
4 4 4
0 1 -2
2 2 1
3 0 5
3 3 7
代码如下:
import java.util.Scanner;
public class Main {
public static class Triple<T> {
int row,column;
T item;
public Triple() {
row = 0;
column = 0;
item = null;
}
public Triple(int row,int column,T item) {
this.row = row;
this.column = column;
this.item = item;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
Triple<Integer>[] triples1 = new Triple[scanner.nextInt()];
Triple<Integer>[] triples2 = new Triple[scanner.nextInt()];
for (int i = 0;i < triples1.length; i ++) {
//这里一定要再初始化一下,否则会报空指针异常错误
triples1[i] = new Triple<>();
triples1[i].row = scanner.nextInt();
triples1[i].column = scanner.nextInt();
triples1[i].item = scanner.nextInt();
}
for (int i = 0;i < triples2.length; i ++) {
triples2[i] = new Triple<>();
triples2[i].row = scanner.nextInt();
triples2[i].column = scanner.nextInt();
triples2[i].item = scanner.nextInt();
}
Triple<Integer>[] triples3 = add(triples1,triples2);
System.out.println(m + " " + n + " " + triples3.length);
for (int i = 0;i < triples3.length; i ++) {
System.out.print(triples3[i].row + " ");
System.out.print(triples3[i].column + " ");
System.out.println(triples3[i].item);
}
}
public static Triple<Integer>[] add(Triple<Integer>[] triples1,Triple<Integer>[] triples2) {
Triple<Integer>[] triples0 = new Triple[triples1.length + triples2.length];
int count = 0;
int i = 0;
int j = 0;
while (i < triples1.length && j < triples2.length) {
if (triples1[i].row == triples2[j].row) {
if (triples1[i].column < triples2[j].column) {
triples0[count] = new Triple<>();
triples0[count].row = triples1[i].row;
triples0[count].column = triples1[i].column;
triples0[count].item = triples1[i].item;
count ++;
i ++;
} else if (triples1[i].column > triples2[j].column) {
triples0[count] = new Triple<>();
triples0[count].row = triples2[j].row;
triples0[count].column = triples2[j].column;
triples0[count].item = triples2[j].item;
count ++;
j ++;
} else {
if (triples1[i].item + triples2[j].item != 0) {
triples0[count] = new Triple<>();
triples0[count].row = triples1[i].row;
triples0[count].column = triples2[j].column;
triples0[count].item = triples1[i].item + triples2[j].item;
count++;
}
i++;
j++;
}
} else if (triples1[i].row > triples2[j].row) {
triples0[count] = new Triple<>();
triples0[count].row = triples2[j].row;
triples0[count].column = triples2[j].column;
triples0[count].item = triples2[j].item;
count ++;
j++;
} else {
triples0[count] = new Triple<>();
triples0[count].row = triples1[i].row;
triples0[count].column = triples1[i].column;
triples0[count].item = triples1[i].item;
count ++;
i++;
}
}
while (i < triples1.length || j < triples2.length) {
if (j == triples2.length) {
triples0[count] = new Triple<>();
triples0[count].row = triples1[i].row;
triples0[count].column = triples1[i].column;
triples0[count].item = triples1[i].item;
count++;
i++;
} else {
triples0[count] = new Triple<>();
triples0[count].row = triples2[j].row;
triples0[count].column = triples2[j].column;
triples0[count].item = triples2[j].item;
count++;
j++;
}
}
Triple<Integer>[] triples = new Triple[count];
for (int k = 0;k < count; k ++) {
triples[k] = new Triple<>();
triples[k].row = triples0[k].row;
triples[k].column = triples0[k].column;
triples[k].item = triples0[k].item;
}
return triples;
}
}