2017-07-28 163 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; 
    }