【串与数组】三元组顺序表表示的稀疏矩阵加法(java)

三元组顺序表表示的稀疏矩阵加法。

输入格式:
输入第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;
    }
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天的命名词

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值