【问题标题】:Find max at row and min at cols in a 2D array在二维数组中的行和列处查找最大值
【发布时间】:2021-04-27 02:25:45
【问题描述】:

我需要编写一个 java 代码,将 2D 数组作为矩阵并检查每一行和每一列,并找到其在行处最大但在列处最小的数字。

例如,13 是 col 的最小值和 row 的最大值:

我写了代码但我迷路了:

import java.util.Scanner;

public class maxmin {
    public static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("enter matrix size ");
        int num = input.nextInt();
        int num1 = input.nextInt();

        maxMin(num, num1);
    }

    private static void maxMin(int num, int num1) {
        int max = 0;
        int min = 0;

        int[][] matrix = new int[num][num1];
        System.out.println("ENTER ARRAY NUMBERS");
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                matrix[i][j] = input.nextInt();
            }
        }

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] >= max) {
                    max = matrix[i][j];
                    for (int a = 0; a < matrix.length; a++) {
                        if (matrix[i][a] <= min) {
                            min = matrix[i][a];
                        }
                        if (max == min) {
                            System.out.println(max);
                        }
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: java arrays multidimensional-array max min


    【解决方案1】:

    您可以为此使用IntStream

    int[][] arr = {
            {1, 2, 3, 6, 5},
            {3, 2, 5, 6, 7},
            {5, 6, 7, 8, 9},
            {1, 3, 0, 2, 4}};
    
    int num = Arrays
            // iterate over the nested arrays,
            // i.e. rows of the 2d array
            .stream(arr)
            // find maximum value of the row
            // return IntStream of maximums
            .mapToInt(row -> Arrays.stream(row)
                    // maximum value of the row
                    .max().orElse(Integer.MIN_VALUE))
            // find minimum value of row maximums
            .min().orElse(Integer.MIN_VALUE);
    
    // output
    System.out.println(num); // 4
    

    【讨论】:

      【解决方案2】:

      所以第一件事。由于您试图在行中查找 MAX 并且在列中查找 MIN 的数字,这意味着您正在尝试查找多个数字。因此,应该有一些重置代码来重置最大值和最小值,以便找到新的最大值和最小值。

      然后,您需要考虑的是如何找到它。由于它必须首先在列中为最大值,因此您必须在记录最大位置时首先迭代每行中的所有位置。然后迭代与该行中的最大位置关联的特定列。你懂了?只有当您先完成一整行并知道位置时,才能再次迭代该列(除了第一次之外)。

      @Alex Rudenko 指出,由于矩阵中有负数,因此 max 和 min 的初始值应默认为

      int max = Integer.MIN_VALUE; 
      int min = Integer.MAX_VALUE; 
      

      这对于以下代码的工作至关重要。

      所以最后一个块的代码应该是这样的:

      for (int i = 0; i < matrix.length; i++){
          for (int j = 0; j < matrix[0].length; j++){
              if (matrix[i][j] > max){
                  max = matrix[i][j];
                  maxPos = j; //declare this beforehand
              }
          } 
          for (int a = 0; a < matrix.length; a++ ){
              if( matrix[a][maxPos] < min){
                  min = matrix[a][maxPos];
              }
          } 
          if(max == min) {
              System.out.println(matrix[i][maxPos]); 
              max = Integer.MIN_VALUE; 
              min = Integer.MAX_VALUE; 
      
          }
      }
      

      就是这样!可能有一些东西可以避免在已检查的位置上重复,但这是此操作的中心逻辑。

      另外,我假设您只打印最大和最小数字。如果您以后需要使用它,您还可以将这些数字保存在数组或其他东西中。

      由于您正在尝试学习,因此我建议您不要使用高级方法。这是巩固你编码肌肉的有用基础。

      【讨论】:

        猜你喜欢
        • 2019-03-31
        • 2017-04-03
        • 1970-01-01
        • 2015-04-06
        • 2016-03-19
        • 1970-01-01
        • 2016-03-16
        • 1970-01-01
        • 2017-07-17
        相关资源
        最近更新 更多