【问题标题】:Find maximal elements in array and delete all lines and columns that contain it查找数组中的最大元素并删除包含它的所有行和列
【发布时间】:2018-03-19 07:39:56
【问题描述】:

如果我们有这些数字

array [][]
{1, 2, 3, 4},
{1, 2, 20, 4},
{1, 20, 2, 4},
{1, 2, 3, 4},};

应该是这样的

1 0 0 4 
0 0 0 0 
0 0 0 0 
1 0 0 4 

但我只能像这样输出代码...

1 0 3 4 
1 0 20 4 
0 0 0 0 
1 0 3 4 

我不明白如何纠正它,请帮助我, 这是我的代码。谢谢!

package com.company;

public class Main {

    public static void main(String[] args) {

        int[][] array2 = {{1, 2, 3, 4},
                {1, 2, 20, 4},
                {1, 20, 2, 4},
                {1, 2, 3, 4},};

        int countMax = 0;
        int countIndexHorizontal = 0;
        int countIndexVertical = 0;
        int max = Integer.MIN_VALUE;
        int m, k,x;

        for (int i = 0; i < array2.length; i++) {
            for (int j = 0; j < array2[i].length; j++) {
                if (array2[i][j] > max) {
                    max = array2[i][j];   
                }
            }
        }

        for (k = 0; k < array2.length; k++) {
            for (m = 0; m < array2[k].length; m++) {
                if (array2[k][m] == max) {
                    countIndexHorizontal = k;
                    countIndexVertical = m;
                    for (x = 0; x < array2.length; x++) {
                        for (int j = 0; j < array2[x].length; j++) {
                            if (countIndexVertical == x || j == countIndexHorizontal) {
                                array2[x][j] = 0;
                            }
                        }
                    }
                }
            }
        }

        for (int i = 0; i < array2.length; i++) {
            for (int j = 0; j < array2[i].length; j++) {
                System.out.print(array2[i][j] + " ");
            }
            System.out.println();
        }
    }
}

看起来有很多代码和一些文字和网站不允许我最终发布我的问题,我真的很生气,也许这篇文字会帮助我解决这个问题。

【问题讨论】:

  • 为什么它看起来像'this'?
  • 因为程序应该找到最大值并删除所有包含最大值的行和列
  • 你想说的是consist而不是contain?如果我们将您的输入理解为矩阵,则最大值为20,因此包含20 的所有行和列的值都必须设置为0
  • 请同时在问题内而不是仅在标题中给出任务。如您所见,否则它会使人们感到困惑。
  • 是的,我想说包含。对不起,英语不是我的母语。好的,下次我会做的。

标签: java


【解决方案1】:

您可以通过将条件更改为来改进程序

if (countIndexHorizontal == x || j == countIndexVertical) 

您的程序正在删除它找到的前 20 行(包括其他 20 行)的错误行。

此更改将为您提供特定案例的正确答案,但对于此类示例,该程序仍然存在问题

2  2
1  1

正确的输出是

0  0
0  0

但它不起作用,因为当它找到第一个 2 时,它将删除第二个 2,但仍然需要第二个 2 来清除右下角的 0。您将不得不为要清除的位置保留一个单独的数据结构。我会留给你弄清楚。

【讨论】:

  • 但是我已经有你写的那种情况或者我不明白你的意思,请发短信详细说明,谢谢!
  • 你的情况是“像”那样,但你的变量是错误的
  • 不!您可以找到几乎相同的行并查看差异
  • 没问题。正如我在回答中所说,它仍然需要一些工作,但你快到了。
  • 你能帮我解决一下吗?)代码中的问题在哪里?
【解决方案2】:

算法

这是您只使用笔和纸时的部分。基本上,我对问题的理解如下:

给定一个矩阵,其中有 n 行和 m 列的Integer1

  1. 找出最大值

  2. 行和列中的所有条目的值都必须设置为 0

  3. 打印更新后的矩阵

假设

我做了以下假设:

  1. n中不一定等于m。例如,您可以拥有数组:

    Valid Input
    1  2  3  4  5  6
    7  8  9 10 11 12
    7  8 12  5  6  4
    

    必须给出,因为 12 是最高值:

    Output
    1  2  0  4  5  0
    0  0  0  0  0  0
    0  0  0  0  0  0
    
  2. 矩阵是一致的:每行有 m 列,每列有 n 行。比如这样的输入是不正确的:

    Incorrect input
    1  2  3
    7  8  9 10
    7  8
    

逻辑

在这个阶段,你仍然只使用笔、纸和谷歌!

在我的问题理解中,我分为三个部分,因为我认为这就是您理解问题的方式。这就是数学的理解。现在让我们把它转换成更Java的理解。首先,我们需要翻译一些词汇:

Mathematics wording |     Java wording
--------------------|---------------------
      matrix        |  2-dimensional array
      Integer       |  int (primitive type)

以Java方式给出:

给定一个二维int[][] 数组:

  1. 找到最高值

  2. 找到值最高的行s和列s

  3. 更新数组,将 2 中的行s 和列s 的值设置为 0。

  4. 打印数组

在我的解决方案的具体情况下,我结合1。 + 2. 和我结合了 3. + 4.

你做了什么

让我们打开你最喜欢的 IDE 并将我的分析与你的输入进行比较:

  1. 找到最大值:在这里确定::您使用变量max 并扫描矩阵以找到最大值。您还假设矩阵可以是矩形(n 行和 m 列,n != m)。所以很好

    for (int i = 0; i < array2.length; i++) {
        for (int j = 0; j < array2[i].length; j++) {
            if (array2[i][j] > max) {
                max = array2[i][j];
            }
        }
    }
    
  2. 找到行s和列sERROR here,正如ccp-beginner所提到的,当您找到了最高值,您擦除(设置为零)整列和整行的值,但可能相同的最高值存储在该行或列中的其他位置

    for (k = 0; k < array2.length; k++) {
        for (m = 0; m < array2[k].length; m++) {
            if (array2[k][m] == max) {
                countIndexHorizontal = k;
                countIndexVertical = m;
                // additional operation defined in 3.
            }
    
            // in ccp-beginner example of
            //  2, 2
            //  1, 1
            // if k=0 and m=0, you'll update the value
            // of the first row and first column giving:
            //  0, 0
            //  0, 1
            // but when k=0, m=1, you'll find 0 instead 
            // of 2 so your program will consider that
            // this row / column does not contain the 
            // highest value
        }
    }
    

    我在这里假设

    • countIndexHorizo​​ntal = 行
    • countIndexVertical = 列

    因此,当您将值设置为 0 时,您需要跟踪行s 和列s

  3. 更新数组此处出现错误(参见ccp-beginner 的回答)

                for (x = 0; x < array2.length; x++) {
                    for (int j = 0; j < array2[x].length; j++) {
                        if (countIndexVertical == x || j == countIndexHorizontal) {
                            array2[x][j] = 0;
    
                            // In your example:
                            // 1  2  3  4
                            // 1  2 20  4
                            // 1 20  2  4
                            // 1  2  3  4
                            // and if countIndexVertical=2 and countIndexHorizontal=1 
                            // (the 20 of the second row between 2 and 4), you'll have
                            // 1  0  3  4
                            // 1  0 20  4
                            // 0  0  0  0
                            // 1  0  3  4
                            // instead of
                            // 1  2  0  4
                            // 0  0  0  0
                            // 1 20  0  4
                            // 1  2  0  4
                        }
                    }
                }
    

    您对countIndexVerticalcountIndexHorizontal 感到困惑,就像您一样

    • x = 行
    • j = 列

    你应该有(请注意交换)

                    if (countIndexHorizontal == x || j == countIndexVertical) {
                        array2[x][j] = 0;
                    }
    
  4. 打印数组:这里可以,没什么特别要提的

    for (int i = 0; i < array2.length; i++) {
        for (int j = 0; j < array2[i].length; j++) {
            System.out.print(array2[i][j] + " ");
        }
        System.out.println();
    }
    

问题

基本上,您需要的是如何存储包含最高值的行和列。起初,我们可能会想再次使用数组,比如

int[] rowsContainingHighestValue;
int[] columnsContainingHighestValue;

但您不知道会遇到多少个最高值,因此您需要一些东西来存储具有动态大小的多个值:我将使用List

一些Java点

rowsContainingHighestValuecolumnsContainingHighestValue 变为:

List<Integer> rowsContainingHighestValue = new ArrayList<>();
List<Integer> columnsContainingHighestValue = new ArrayList<>();

您可能想看看以下几点:

  1. 为什么是List instead of array
  2. 我的对象被声明为List,但我实例化2ArrayList:What is a Interface and what is a Class
  3. 为什么我使用List&lt;Integer&gt; instead of List&lt;int&gt;
  4. 什么是the difference between Integer and int

一个解决方案

  1. 循环遍历矩阵以获取最大值并存储 所有 行和保存该值的行和列,而不是单个行/列组合(countIndexHorizontalcountIndexVertical
    1. 如果找到新的最大值,则存储它(就像您所做的那样)AND 存储当前行和列索引
    2. 如果某个值与当前最大值相同(例如,您有两次20),则将行和列索引追加到现有的相应行/列列表
  2. 第二次循环以更新和打印值
    1. 它基本上结合了你最后的两个双循环:如果扫描的元素属于一行保存最大值的列,则该值必须设置为 0(完全按照你的方式但更短,因为我已经有了行/列的列表)
    2. 正确更新值后,只需进行简单的打印

代码中给出的内容:

public static void main(String[] args) {

    int[][] array2 = {
        {1, 2, 3, 4},
        {1, 2, 20, 4},
        {1, 20, 2, 4},
        {1, 2, 3, 4}
    };

    // find the maximum value and store its position:
    // It's List instead of a single value as multiple 
    // rows and columns can hold the same maximum value
    List<Integer> rowsWithMaxValue = new ArrayList<>();
    List<Integer> colsWithMaxValue = new ArrayList<>();
    int maxValue = Integer.MIN_VALUE;

    // First matrix-scan to fetch the maximum value and the
    // row(s) and column(s) to set the value at 0
    for (int row = 0; row < array2.length; row++) {
        for (int col = 0; col < array2[row].length; col++) {

            // get the current value
            int value = array2[row][col];

            // found a new maximum or an existing one?
            if (value > maxValue) {
                // this is a new maximum value, we can reset
                // the list as the previous rows and columns
                // are not relevant anymore
                maxValue = value;
                rowsWithMaxValue = new ArrayList<>();
                colsWithMaxValue = new ArrayList<>();
                rowsWithMaxValue.add(row);
                colsWithMaxValue.add(col);
            } else if (value == maxValue) {
                // The same value (like 20) is found again
                // so multiple rows and columns will have 
                // their value set at 0
                rowsWithMaxValue.add(row);
                colsWithMaxValue.add(col);
            }
        }
    }

    // Second matrix-scan for updating the values and printing
    for (int row = 0; row < array2.length; row++) {
        for (int col = 0; col < array2[row].length; col++) {
            // is it in a forbidden row? If yes, set the value
            // at zero. One of the condition (row or column) is
            // enough to have its value set at 0
            if (rowsWithMaxValue.contains(row) || colsWithMaxValue.contains(col)) {
                array2[row][col] = 0;
            }
            
            // Simply print the value
            System.out.print(array2[row][col] + " ");
        }
        System.out.println();
    }
}

1数学意义中的整数:不带小数的正数或负数

2这里就不解释实例化了。随意谷歌它

【讨论】:

  • 我学习 Java 才一个月,我对你写的很多东西都不太了解:
  • @AndrewStill,我已经更新了我的答案,我认为它非常详尽。我希望你能理解。请记住,我不会取代任何教学,否则它将是 10 美元 + 每小时一瓶啤酒。我在开玩笑,但你明白了。快乐阅读和快乐编码!如果我的回答对你有帮助,请采纳/点赞
  • 这真的可以理解而且非常详尽!非常感谢!
  • 我们很欢迎您,也很高兴它对您有所帮助。如果对您有用,请随意投票 cpp 的答案(您可以接受一个答案但支持多个答案)
  • 我的声望不够(
猜你喜欢
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
相关资源
最近更新 更多