在二维数组中的列处查找最大值和最小值

时间:2021-01-22 15:41:52

标签: java arrays multidimensional-array max min

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

例如,13 是列的最小值和行的最大值:

the 13 is the min at col and max at the 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);
                        }
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以将 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

答案 1 :(得分:-1)

所以第一件事。由于您正在尝试查找行为 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; 

    }
}

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

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

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

相关问题