如果数字在矩阵的所有列中,我怎样才能成真?

时间:2018-03-12 18:19:53

标签: java matrix

我的问题很简单,如果我的数字在矩阵的所有列中,我只想返回true,例如3x3。

public class matrixcolumns {

public static void main(String[] args) {
    int [][] matrix = {{3,2,4},{5,2,6},{7,8,2}};
    int num = 2;
    /*Scanner sc = new Scanner(System.in);
    System.out.print("Introduce a number: ");
    num = sc.nextInt();*/
    System.out.println("The result is: "+checkcolumns(matrix, num));
}

public static boolean checkcolumns(int matrix[][], int num){
    int cont = 0;
    for (int x = 0; x < matrix.length; x++) {
        for (int y = 0; y < matrix[x].length; y++) {
            if (num == matrix[x][y]){
                cont++;
            }
        }
    }
    if (cont == 3){
        return true;
    }
    else{
        return false;
    }
}

}

2 个答案:

答案 0 :(得分:0)

如果找到你的号码,你会将续数增加1并打破内循环。

答案 1 :(得分:0)

在此之前,请关注Java naming conventions

  • firstWordLowerCaseVariable
  • firstWordLowerCaseMethod()
  • FirstWordUpperCaseClass
  • ALL_WORDS_UPPER_CASE_CONSTANT

并且始终如一地使用它们,这将使您和我们更容易阅读您的代码。

接下来,你的if声明什么都不做,我想是因为你不知道如何处理它。

好吧,如果您添加count++ 可以工作,但如果每列有多个2,该怎么办?然后你的算法将无法正常工作

因此,对于这种情况,您需要修复算法,我实际上是通过以下代码实现的:

public class MatrixColumns {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3}, 
            {2, 0, 0}, 
            {0, 0, 2}
        };

        int[][] secondMatrix = {
            {1, 2, 3}, 
            {2, 2, 2}, 
            {0, 0, 0}
        };

        int[][] thirdMatrix = {
            {2, 2, 2}, 
            {2, 2, 2}, 
            {2, 2, 2}
        };

        int num = 2;
        System.out.println("The result is: " + checkColumns(matrix, num, false));
        System.out.println("The result is: " + checkColumns(secondMatrix, num, false));
        System.out.println("The result is: " + checkColumns(thirdMatrix, num, false));

        System.out.println("The result is: " + checkColumns(matrix, num, true));
        System.out.println("The result is: " + checkColumns(secondMatrix, num, true));
        System.out.println("The result is: " + checkColumns(thirdMatrix, num, true));
    }

    //This method returns the column in which the number was found and -1 if it wasn't found in any column
    private static int checkRow(int[] row, int num, int startFrom) {
        for (int i = startFrom == -1 ? 0 : startFrom; i < row.length; i++) {
            if (row[i] == num) {
                return i;
            }
        }
        return -1;
    }

    //If there can be more than 1 number 2 in each column, we set "allowRepeatInRow" to true and it will look for all the column values, otherwise set it to false
    public static boolean checkColumns(int matrix[][], int num, boolean allowRepeatInRow) {
        boolean[] results = new boolean[matrix.length]; //Array where we will store the results of each column

        int column = 0;
        int nextColumn = 0;
        for (int i = 0; i < matrix.length; i++) {
            if (allowRepeatInRow) {
                do {
                    column = checkRow(matrix[i], num, nextColumn);
                    if (column != -1) {
                        results[column] = true; //We change the value to "true" on the column where the number was found for each row.
                    }
                    nextColumn = column + 1;
                } while (column < matrix.length - 1 && column != -1);
            } else {
                column = checkRow(matrix[i], num, 0);
                if (column != -1) {
                    results[column] = true; //We change the value to "true" on the column where the number was found for each row.
                }
            }
        }

        //We iterate over the results array, if any of them is "false" we return false as there is at least one column where the number was not found   
        for (int i = 0; i < results.length; i++) {
            if (!results[i]) {
                return false;
            }
        }

        return true; //We return true if the above method didn't found any false column, and thus all columns have at least one number "2"
    }
}

上述计划的结果如下:

The result is: true
The result is: false
The result is: false
The result is: true
The result is: true
The result is: true

为什么它会为secondthird矩阵生成2个不同的结果?而不是第一个?

  • 第一个矩阵在每列中都有一个2
  • 第二个矩阵在第一行中有1 2,在第二行中有3 2,然后它返回找到的第一个2的索引。如果你不继续寻找其余的列,它只会跳过剩下的列。
  • 第二个矩阵与第二个矩阵相同,如果只返回第一个矩阵,它将跳过第2列和第3列。

希望这有帮助。