【问题标题】:Find the smallest elements in a matrix and check if they already exist查找矩阵中的最小元素并检查它们是否已经存在
【发布时间】:2021-05-13 06:10:26
【问题描述】:

我有一个二维矩阵。这正好有四行和四列。我现在想创建一个报价,其中包含四个元素。 报价可以按如下方式计算:Offer1 = Matrix_ce + Matrix_ef + Matrix_fg = (c, e, f, g)。下面的成本矩阵可以在下面看到。我现在正在寻找成本最低的报价。报价现在可能包含不相等的数字,即它可能不包含相同的数字。正式说c ≠ e ≠ f ≠ g

// Note: It looks like every row is the same, but only in this example.
// I have another matrix where all rows are different.
Matrix =  [[14 16 18 20]
          [14 16 18 20]
          [14 16 18 20]
          [14 16 18 20]]

计算示例:

如果我现在考虑报价,即[1 2 3 4],如果我们现在将报价应用于Matrix,计算成本的过程如下。

对于 A:

  • 首先,我转到第 1 行并选择那里的第二个元素(在本例中为成本矩阵 A 中的数字 2)。
  • 然后在第 2 行第 3 列(值:7)
  • 然后在第 3 行第 4 列(值:12)

因此,优惠的费用为Offer = 16 + 18 + 20 = 54。这些都很高,所以寻找尽可能小的订单。例如,更好的报价是Offer = [4 1 2 3] = 14 + 16 +18 = 48

MatrixMatrix with explanation

我要找我要找总量少的项目。

我现在如何创建尽可能低的报价?我已经开始创建两个循环,并且在这个循环中总是应该找到矩阵中的最小元素,因此不得违反以下条件。例如:Offer1 = Matrix_ce + Matrix_ef + Matrix_fg = (c, e, f, g), c ≠ e ≠ f ≠ g。这种具有 [1 1 1 1] 或 [1 2 3 1], ... 的解决方案是不正确的。有谁知道如何计算这个?有几种解决方案,因此有多种报价。但是,我只是在寻找一个。

int[][] matrix = new int[][]{[14,16,18,20],
                             [14,16,18,20],
                             [14,16,18,20],
                             [14,16,18,20]}
int[] elements = new int[matrix.length];
int[] offer = new int[matrix.length];
for(int i=0; i<matrix.length; i++) {

  for(int j=0; j<matrix[i].length; i++) {
     // What is the best way to find the smallest elements?
     Check that values are not duplicated, if the value is already present, skip it.
     //if(Arrays.asList(elements).contains(matrix[i][j])) {
     //  continue;
     //}

  }

}
System.out.println(Arrays.toString(offer));

【问题讨论】:

标签: java matrix matrix-multiplication


【解决方案1】:
int min = matrix[0];
int lowestIndex = 0;
for(int j = 1; j < matrix[i].length; j++){
    if(matrix[I][j] < min){
        lowestIndex = j;
        min = matrix[j];
    }
}

【讨论】:

  • 感谢您的回答。我只是不太明白如何找到解决方案?
  • 您是否只想找到二维数组中的最小元素或元素?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 2014-05-09
  • 1970-01-01
相关资源
最近更新 更多