【问题标题】:Cannot calculate correctly a 2d array in Java无法在 Java 中正确计算二维数组
【发布时间】:2018-05-29 19:50:14
【问题描述】:

好吧,我有这部分程序,但无论我做什么,我都无法获得我期望的输出。我拼命尝试使用 8 'println' 来找出我的错误,但仍然无法弄清楚我应该做什么。虽然我认为问题从 k .. k=1 的第二个值开始。

   public static void main(String[] args) throws IOException {

      String path = args[0];
      BufferedReader br = null;
      String line;

       int count = 0;
       while ((line=br.readLine()) != null) {

              if (count==0) {
                 int n = Integer.parseInt(line);
             }

            else if (count==1) {
                 int m = Integer.parseInt(line);
                 int Cost = new int[n][m];
                 int VMs = new int[m][m];
            }

           else if (count>=3 && count<n+3) { 
                   String[] Spliter = line.split("\\s+"); 
                   for (int j = 0; j < m; j++) {
                 String str = Spliter[j];
                 int x = Integer.parseInt(str);
                 Cost[count-3][j] = x;
                   }
           }

           else if (count>=n+4 && count<=n+4+m) {
                   String[] Spliter = line.split("\\s+");
                   for (int k = 0; k < m; k++) {
                       String str = Spliter[k];
                       int x = Integer.parseInt(str);
                       Vms[count-n-4][k] = x; 
                    }
           }

           count++;
         }

           int[][] NewCost = new int[n][m];
                for (int w = 0; w<m; w++){
                    NewCost[0][w]=Cost[0][w];
           }

           for (int i = 1; i<n; i++){
                for (int j = 0; j<m; j++){
                   int a = 10000;

                   for (int k = 0; k<m; k++){
                       y = Cost[i-1][k] + Cost[i][j] + Vms[k][j];

                       if (y < a) {
                          NewCost[i][j] = y;
                          a = y;
                       }
                   }
                }
            }

           for (int i = 0; i<n; i++) {
               for (int j = 0; j<m; j++) {
                 System.out.print(NewCost[i][j] + " ");
               }
                  System.out.print("\n");
         }
      }  

对于下面的输入( 是我的解释,不包含在输入文本中)

4     <--that's my n
3     <--that's my m

5 6 3
7 8 5    <--that's the *Cost* array
7 8 3
2 7 6

0 7 2
7 0 2    <--that's the *VMs* array
2 2 0 

我应该得到这样的东西

5 6 3 
12 13 8     <-- *NewCost* array
17 18 11
15 20 17

但是我得到了这个

5 6 3 
12 13 8
14 15 8
7 12 9

计算示例:

NewCost[0][j] = Cost[0][j]     <-- this must be true in every possible input. 
To be specific, in this example we want the first row NewCost[0][j] = {5 , 6 , 3}

NewCost[1][0] = min(X1,X2,X3) = min(12,20,12) = 12
X1=Cost[0][0]+Cost[1][0]+VMs[0][0] = 5+7+0 = 12
X2=Cost[0][1]+Cost[1][0]+VMs[1][0] = 6+7+7 = 20
X3=Cost[0][2]+Cost[1][0]+VMs[2][0] = 3+7+2 = 12

NewCost[1][1] = min(Y1,Y2,Y3) = min(20,14,13) = 13
Y1=Cost[0][0]+Cost[1][1]+VMs[0][1] = 5+8+7 = 20
Y2=Cost[0][1]+Cost[1][1]+VMs[1][1] = 6+8+0 = 14
Y3=Cost[0][2]+Cost[1][1]+VMs[2][1] = 3+8+2 = 13

NewCost[1][2] = min(Z1,Z2,Z3) = min(12,13,8) = 8
Z1=Cost[0][0]+Cost[1][2]+VMs[0][2] = 5+5+2 = 12
Z2=Cost[0][1]+Cost[1][2]+VMs[1][2] = 6+5+2 = 13
Z3=Cost[0][2]+Cost[1][2]+VMs[2][2] = 3+5+0 = 8

So the second row will be NewCost[1][j] = {12 , 13 , 8}

NewCost[2][0] = min(X1,X2,X3) = min(19,27,17) = 17
X1=Cost[1][0]+Cost[2][0]+VMs[0][0] = 12+7+0 = 19
X2=Cost[1][1]+Cost[2][0]+VMs[1][0] = 13+7+7 = 27
X3=Cost[1][2]+Cost[2][0]+VMs[2][0] = 8+7+2 = 17

NewCost[2][1] = min(Y1,Y2,Y3) = min(27,21,18) = 18
Y1=Cost[1][0]+Cost[2][1]+VMs[0][1] = 12+8+7 = 27
Y2=Cost[1][1]+Cost[2][1]+VMs[1][1] = 13+8+0 = 21
Y3=Cost[1][2]+Cost[2][1]+VMs[2][1] = 8+8+2 = 18

NewCost[2][2] = min(Z1,Z2,Z3) = min(17,18,11) = 11
Z1=Cost[1][0]+Cost[2][2]+VMs[0][2] = 12+3+2 = 17
Z2=Cost[1][1]+Cost[2][2]+VMs[1][2] = 13+3+2 = 18
Z3=Cost[1][2]+Cost[2][2]+VMs[2][2] = 8+3+0 = 11

So the third row will be NewCost[2][j] = {17 , 18 , 11}

NewCost[3][0] = min(X1,X2,X3) = min(19,27,15) = 15
X1=Cost[2][0]+Cost[3][0]+VMs[0][0] = 17+2+0 = 19
X2=Cost[2][1]+Cost[3][0]+VMs[1][0] = 18+2+7 = 27
X3=Cost[2][2]+Cost[3][0]+VMs[2][0] = 11+2+2 = 15

NewCost[3][1] = min(Y1,Y2,Y3) = min(31,25,20) = 20
Y1=Cost[2][0]+Cost[3][1]+VMs[0][1] = 17+7+7 = 31
Y2=Cost[2][1]+Cost[3][1]+VMs[1][1] = 18+7+0 = 25
Y3=Cost[2][2]+Cost[3][1]+VMs[2][1] = 11+7+2 = 20

NewCost[3][2] = min(Z1,Z2,Z3) = min(25,26,17) = 17
Z1=Cost[2][0]+Cost[3][2]+VMs[0][2] = 17+6+2 = 25
Z2=Cost[2][1]+Cost[3][2]+VMs[1][2] = 18+6+2 = 26
Z3=Cost[2][2]+Cost[3][2]+VMs[2][2] = 11+6+0 = 17

So the forth row will be NewCost[3][j] = {15 , 20 , 17}

【问题讨论】:

  • 请关注Java naming conventionsfirstWordLowerCaseVariableFirstWordUpperCaseClassfirstWordLowerCaseMethod()ALL_WORDS_UPPER_CASE_CONSTANT 并始终如一地使用它们,这样代码对您和我们来说都更具可读性。
  • 您能阅读minimal reproducible example 并发布一份吗?我们也缺少mainCostVMs 的声明。
  • 您能否更好地解释一下计算是如何完成的?使用您的数组索引和数字?或绘图/图表。目前还不清楚
  • 您的算法会怎样计算NewCost[x][y]?一个例子是没有帮助的。
  • 最有帮助的是解释您打算如何计算当前正确计算的值之一,即底部两行之一.你打算在那里做什么样的数学并不明显。

标签: java arrays loops for-loop dynamic-programming


【解决方案1】:

我们仍然不能真正理解您要解决的问题,但从您的示例计算中可以看出,您希望在添加中包含的第一个值是最外层循环的上一次迭代的优化值。您的问题是您仅在 NewCost 中保存了上一次迭代,但仅从 Cost 读取值。

对于NewCost[2][0],您计算了X3=Cost[1][2]+Cost[2][0]+VMs[2][0] = 8+7+2 = 17,这是不准确的。 Cost[1][2] 是 5,而不是 8,因为它来自原始的 Cost 数组。如果您希望从上一次迭代中检索 8,则需要从保存该结果的 NewCost 数组中查找它。

这是动态规划的重点,在下一轮计算中使用您最近计算的最优值。但它只有在您使用最近的计算时才有效!

尝试改变

y = Cost[i-1][k] + Cost[i][j] + Vms[k][j];

y = NewCost[i-1][k] + Cost[i][j] + Vms[k][j];

【讨论】:

  • 天哪!我的眼前有了答案,却想不出来。首先非常感谢大家的帮助!好吧,该程序就像在 M 台机器上运行的 N 个作业的虚拟显示,我想找到在每种机器类型中运行的作业的最低成本。 NxM 数组表示作业在每台机器上运行的成本,MxM 数组表示切换机器类型的成本。我不认为这有帮助,因为我的英语不是最好的,所以我真的希望你能以某种方式理解这个程序是关于什么的。再次非常感谢!
  • 很高兴为您提供帮助,欢迎来到 Stack Overflow。这实际上确实有助于澄清问题,并且会对问题进行很好的编辑,以防出现任何其他更彻底的答案。如果这个答案或任何未来更好的答案解决了您的问题,请将一个标记为已接受。
  • 不知道我必须标记。我标记了您的解决方案,因为它运行良好!
猜你喜欢
  • 2010-10-30
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多