2017-07-28 161 views
1

我很抱歉,我不确定标题是否正确,如果不正确,我会在有人告诉我这是什么时调整它。正如你能理解我的新节目...Java中的递归递归

我要做到以下几点: 我有一个循环:

for(int i=0; i < this.matrix.length; i++) 

我会有这样的例如矩阵:

1, 2, 2 
2, 2, 3 
0, 1, 2 

我想乘以对角元素1 * 2 * 2 我知道如何获取这些元素的每个循环的步骤,但我怎样才能使用一个临时变量,每一步将乘以新元素?或者这是不可能的?

比如我做一个变量:

double temp; 

每个循环步骤我希望新的数值由老倍增,但保留的价值,不知道如果我解释这一点。 但是如果我们用这个矩阵我想是这样的:

temp = 1; 

下一步它

temp = 2; 

下一步

temp = 4; 

我试着这样做我自己,但最终会得到错误的结果,我知道我在做错误的乘法,因为当我将矩阵的2 2元素更改为3而不是2时,我的最终结果将是9而不是6. 我很抱歉,如果这是不好解释...

+3

循环为'INT温度之前声明变量= 1'。在循环中只需将它与正确的数组位置相乘即可。此外,你还应该添加你的代码,这使得它更容易解释你在那里做错了什么。 – SomeJavaGuy

+0

像'temp * = matrix [i] [i];'在循环内部就可以了。 – Henry

+1

你能提供你的代码吗?夏娃,如果它是错误的 –

回答

2
  1. 在你的问题,你只要求主左到右对角线输出倍增,所以我假设这是你唯一的目标。
  2. 另外,你没有指定矩阵是否总是正方形;我会承认是的。
  3. 最后,你没有指定这个矩阵如何完全存储在变量中。我假设我们正在讨论一个二维数组。

这里,我们去:

public static void main (String[] args) throws Exception { 
    int[][] matrix = new int[3][]; 
    matrix[0] = new int[] {1, 2, 2}; 
    matrix[1] = new int[] {2, 2, 3}; 
    matrix[2] = new int[] {0, 1, 2}; 

    int result = 1; 
    for (int i=0; i<matrix.length; i++) { 
     result *= matrix[i][i]; 
    } 
    System.out.println(result); 
} 

编辑:如果你想也包括从右至左:

public static void main (String[] args) throws Exception { 
    int[][] matrix = new int[3][]; 
    matrix[0] = new int[] {1, 2, 2}; 
    matrix[1] = new int[] {2, 2, 3}; 
    matrix[2] = new int[] {0, 1, 2}; 

    int resultL2R = 1; 
    int resultR2L = 1; 
    for (int i=0; i<matrix.length; i++) { 
     resultL2R *= matrix[i][i]; 
     resultR2L *= matrix[i][matrix.length-1-i]; 
    } 
    System.out.println("left-to-right: " + resultL2R); 
    System.out.println("right-to-left: " + resultR2L); 
} 
+0

是啊抱歉没有提到任何这些东西,但你认为一切正确。 但是对于我的下一步,我也将尝试从右到左走对角线。如果从左到右是相当直接的,那么反过来就会让我感到困惑。 关于矩阵是否正方形,我猜测它总是正方形的,除非你能得到一个不是正方形的逆矩阵。我的最终结果是制作一个逆矩阵方法。 –

1

我猜你想有这样的解决方案:

public static void main (String [] args) 
{ 
    int[][] matrix = new int[][] { 
      {1, 2, 2}, 
      {2, 2, 3}, 
      {0, 1, 2} 
    }; 

    int result = 1; 

    for(int i = 0; i < matrix.length; i++){ 
     result = result * matrix[i][i]; 
    } 

    System.out.println("Result: " + result); 

} 

既然你声明result变量你进入for循环之前,它会保留循环内部评估的值。

+0

伟大的相同的反应,在同一时刻:) –

+0

谢谢,是的,这是我想要的,我不知道如何正确地得到结果值正确,其他部分我想通了。 感谢这样的快速反应大家 –

1

您在环路中的[I] [I]元素

int[][] array= { 
    {1,2,2}, 
    {2,2,3},  
    {0,1,2} 
}; 

int result=1; 
for (int i = 0; i < array.length ; i++) { 
    result=result*(array[i][i]); 
} 
System.out.println("Result "+result); 
+0

谢谢,是的,这是我想要的,我不知道如何正确地得到结果值正确,其他部分我想通了。 –

1

对角线乘法您可以使用下面提及的代码

public static void main(String[] args) { 
    //2D Array 
    int a[][]={{1,2,3},{2,3,4},{3,4,5}}; 
    int multiplier=1; 
    for(int i=0;i<a.length;i++){ 
     multiplier=multiplier*a[i][i]; 
    } 
    System.out.println(multiplier); 
} 
0

好吧,我觉得这个代码你想要做什么了上述矩阵:

int temp=1; 
for(int r=0; r<3; r++)//traversing through row 
{ 
    for(int c=0; c<3; c++)//traversing through column 
    { 
    if(r==c)// condition for diagonal 
     temp*=array[r][c]; 
    }// c close 
    System.out.println("Multiplication value after row "+(r+1)+" = "+temp); 
}// r close 
0

至于你的问题标题所说,你用递归moltiply想。

函数调用自身的可能性被称为递归。

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    int[][] array= { 
      {1, 2, 2}, 
      {2, 2, 3}, 
      {0, 1, 2} 
      }; 

      if(array[0].length==array.length) // check if numbers of columns == rows 
      System.out.println("Result "+multiD(0, array)); 
      else 
      System.out.println("No matrix NxN"); 
} 

public static int multiD(int pos, int [][] m) { 
     if (pos == m.length) {//get out after the last element (multiply for 1) 
      return 1; 
     } else 
      if (m[pos][pos] == 0) {// get out if we found a 0 value , so we don't need to go forward 
       return 0; 
      } else 
     return m[pos][pos] * multiD(pos+1,m); //calculate the result 
    } 

假设你有一个矩阵 “M” 1000×1000和第m [0] [0]是0,则将迭代 1000倍时的结果在开始时是已知的。 为了防止这种情况,你应该写的东西likethis:

(在其他的答案失踪)

int result = 1; 
    for (int i=0; i<matrix.length; i++) { 
     result *= matrix[i][i]; 
     if(result == 0) 
      break; 
    }