在一个二维数组中找到行中的最大值和列中的最小值。

3
我需要编写一个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个回答

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
首先,由于您正在尝试查找在行中为MAX,在列中为MIN的数字,这意味着您正在尝试查找多个数字。因此,应该有一些重置代码来重置最大值和最小值以便查找新的最大值和最小值。
然后,您需要考虑的是如何找到它。由于它必须首先在列中为最大值,因此您必须首先迭代每行中的所有位置并记录最大位置。然后,您迭代与该行中最大位置相关联的特定列。明白了吗?仅当您完成整个行并知道位置时,才会再次迭代该列(除第一次之外)。
@Alex Rudenko指出,由于矩阵中存在负数,因此最大值和最小值的初始值应默认为
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; 

    }
}

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

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

由于您正在尝试学习,我建议不要使用高级方法。这是一个有用的基础,可以巩固您的编码能力。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接