如何从2d矩阵中删除仅包含零的行和列? [英] How to remove rows and columns containing only zeros from a 2d matrix?

查看:52
本文介绍了如何从2d矩阵中删除仅包含零的行和列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个大小为例如 M x N 的整数矩阵,您必须编写一个程序来删除由全零组成的所有行和列.您的程序必须删除仅包含零值元素的那些行和列.也就是说,如果一行(列)中的所有元素均为零,则删除该行(列).所有剩余的行和列都应该输出.

Given an integer matrix of size, say, M x N, you have to write a program to remove all the rows and columns consisting of all zeros. Your program must remove those rows and columns that consist of zero valued elements only. That is, if all the elements in a row (column) are zeros, then remove that row (column). All the remaining rows and columns should be output.

输入规范:

  • 输入的第一行将包含两个整数,例如 M N . M 指定矩阵中的行数,而 N 指定矩阵中的列数.
  • 这之后是 M 行,每行由 N 个整数组成.注意:假设 1< = M< = 10 1< = N< = 10 .另外,您可以假设给定矩阵中至少有一个非零元素.
  • The first line of input will have two integers, say, M and N. M specifies the number of rows in the matrix and N specifies the number of columns in the matrix.
  • This will be followed by M lines, each consisting of N integers. Note: Assume 1 <= M <= 10 and 1 <= N <= 10. Also, you may assume that there is at least one non-zero element in the given matrix.

输出规范:

  • 输出应为结果矩阵.
  • 输出矩阵的每一行都应以换行符结尾.
  • 每一行的连续元素之间应该正好有一个空格(不是制表符).

示例 1:

Sample Input:
4 4
1 2 3 4
0 0 0 0
5 6 7 8
0 0 0 3

Sample Output:
1 2 3 4
5 6 7 8
0 0 0 3

示例2:

Sample Input:
4 5
1 2 0 3 5
0 0 0 0 0
4 5 0 6 9
7 8 0 9 1

Sample Output:
1 2 3 5
4 5 6 9
7 8 9 1

推荐答案

我找到的解决方案如下.它仅适用于具有非负数或非正数的行和列:

The solution I found was as follows. It works only for rows and columns having either non-negative or non-positive numbers:

import java.util.Scanner;

public class RemoveZeroFromMatrix {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("enter rows and column");
        int m = s.nextInt();
        int n = s.nextInt();
        int[][] a = new int[m][n];
        int[][] b = new int[m][n];
        int[][] c = new int[m][n];
        System.out.println("enter the matrix");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = s.nextInt();
            }
        }
        System.out.println("input matrix");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
        int sumRow = 0, x = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                sumRow += a[i][j];
            }
            if (sumRow != 0) {
                for (int k = 0; k < n; k++) {
                    b[i - x][k] = a[i][k];
                }
            } else {
                x++;
            }
            sumRow = 0;
        }
        int sumCol = 0, y = 0, temp = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n - x; j++) {
                sumCol += b[j][i];
            }
            if (sumCol != 0) {
                for (int k = 0; k < n - x; k++) {
                    if (temp == 0) {
                        c[k][i] = b[k][i];
                    }
                    if (temp == 1 && n > 3) {
                        c[k][i] = b[k][i + 1];
                    }
                }
            } else {
                for (int k = 0; k < n - x; k++) {
                    c[k][i] = b[k][i + 1];
                    temp = 1;
                }
            }
            sumCol = 0;
        }
        System.out.println("outut matix:");
        for (int i = 0; i < m - x; i++) {
            for (int j = 0; j < n - y - temp; j++) {
                System.out.print(c[i][j] + " ");
            }
            System.out.println();
        }
    }
}

这篇关于如何从2d矩阵中删除仅包含零的行和列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆