猿问

测试二维数组的每一行和每一列中元素的存在 - java

我编写了一个简短的程序,它随机填充一个 10x10 的 2D 数组,其中包含 0 和 1。我想检查数组以查看每行和每列是否包含超过五个 1。


我已经使用 2 个单独的嵌套 for 循环完成了它,但是有没有办法只使用 1 个嵌套的 for 循环检查每一行和列并打印出每行/列的结果?我无法找到一种将我拥有的内容组合到一个嵌套循环中的方法。非常感谢您的任何建议:)


这是我的代码:


public class Main {


public static void main(String[] args) {


    int[][] array = new int[10][10];

    for (int i = 0; i < array.length; i++) {

        for (int j = 0; j < array.length; j++) {

            array[i][j] = (int) (2 * Math.random());

        }

    }

    System.out.print("---THE MATRIX---\n");

    System.out.println();

    for (int[] arr : array) {

        for (int value : arr) {

            System.out.print(value + " ");

        }

        System.out.println();

    }

    System.out.println();

    fiveOnes(array);

}



private static void fiveOnes(int[][] array) {

    System.out.println("---MORE THAN FIVE ONES THROUGHOUT THE MATRIX---\n");


    for (int i = 0; i < array.length; i++) {

        int oneCount = 0;

        boolean flag = false;

        for (int j = 0; j < array.length; j++) {

            if (array[i][j] == 1) {

                oneCount++;

            }

        }

        if (oneCount > 5)

            flag = true;

        System.out.println("Row " + (i + 1) + ": " + flag);

    }

    System.out.println();


    for (int i = 0; i < array[0].length; i++) {

        int oneCount = 0;

        boolean flag = false;

        for (int j = 0; j < array.length; j++) {

            if (array[j][i] == 1) {

                oneCount++;

            }

        }

        if (oneCount > 5) {

            flag = true;

        }

        System.out.println("Column " + (i + 1) + ": " + flag);

    }

    System.out.println();

}

}


GCT1015
浏览 163回答 3
3回答

莫回无

for (int i = 0; i < 10; i++) {&nbsp; &nbsp; int oneCountRow = 0;&nbsp; &nbsp; int oneCountColumn = 0;&nbsp; &nbsp; for (int j = 0; j < 10; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (array[i][j] == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oneCountRow++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (array[j][i] == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oneCountColumn++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("Row " + (i + 1) + ": " + (oneCountRow > 5));&nbsp; &nbsp; System.out.println("Column " + (i + 1) + ": " + (oneCountColumn> 5));}或免费尺寸:&nbsp; &nbsp; int maxLenght = 0;&nbsp; &nbsp; for (int i = 0; i < array.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; maxLenght = Math.max(maxLenght, array[i].length);&nbsp; &nbsp; }&nbsp; &nbsp; maxLenght = Math.max(maxLenght, array.length);&nbsp; &nbsp; for (int i = 0; i < maxLenght; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int oneCountRow = 0;&nbsp; &nbsp; &nbsp; &nbsp; int oneCountColumn = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < maxLenght; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j < array[i].length && i < array.length && array[i][j] == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oneCountRow++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i < array[j].length && j < array.length && array[j][i] == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oneCountColumn++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Row " + (i + 1) + ": " + (oneCountRow > 5));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Column " + (i + 1) + ": " + (oneCountColumn> 5));}

HUH函数

以下是我对解决方案的看法:您可以创建一个大小为“计数数组”的“计数数组”,max(n, m) * 2其中n和m是矩阵的维度,并使用0s对其进行初始化。该2代表它的数列或行是否(如指数0代表列和1行)。然后,当您找到 a&nbsp;1(对于列和行)时,增加该数组中的代表值。找到正确的索引只是一些模块化算法的问题。

慕容森

您可以尝试在两个平面数组中收集布尔值。一种用于列,一种用于行。然后您只需要一个嵌套循环并为这两个平面数组使用适当的索引。
随时随地看视频慕课网APP

相关分类

Java
我要回答