2012-02-22 71 views
0

我試圖找到二維數組(Java)中的最小值。獲取二維數組中列的最小值

我正在嘗試使用for循環。我的目標是創建一個程序來獲得矩陣的鞍點,但我會一步步地進行。我已經想出瞭如何在二維數組的一行中找到最大值。然後我想在最大值的同一列找到最小值。

這裏是我的代碼:

class Matrix_1 { 
    int[][] A; 
    int a; 
    int b; 
    int max; 
    int min; 
    int i; 
    int d; 

    Matrix_1() { 
     super(); 
    } 

    public int[][] createMatrix(int a, int b) { 
     Scanner inputm = new Scanner(System.in); 
     A = new int[a][b]; 
     System.out.println("Enter elements for matrix A : "); 
     for (int i = 0; i < A.length; i++) { 
     System.out.println("Enter numbers for " + i + "th row"); 
     for (int j = 0; j < A[i].length; j++) { 
      A[i][j] = inputm.nextInt(); 

     } 

     } 
     return A; 
    } 

    public int[][] displayMatrix() { 
     System.out.println("Matrix A: "); 

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

     return A; 
    } 

    public int getMax(int c) { 
     max = 0; 
     a = c; 

     for (i = 0; i < A[a].length; i++) { 

     while (A[a][i] > max) { 
      max = A[a][i]; 
     } 
     } 

     i = d; 
     System.out.println(i); 
     System.out.println("max = " + max); 
     return max; 
    } 

    public int getMin() { 
     max = min; 
     a = 0; 

     for (int j = 0; j < A[a].length; j++) { 
     while (A[i][j] < min) { 
      min = A[j][d]; 
     } 
     } 

     System.out.println("min in row " + a + "from max number" + max + " = " 
      + min); 
     return min; 
    } 
} 
+0

前兩個方法工作正常。 – 2012-02-22 19:36:03

+1

@deporter它是,但至少我已經嘗試過它自己。我只是想要一些幫助。 – 2012-02-22 19:57:18

+0

嘗試將getMin()中的max = min更改爲min = max。 – 2012-02-22 19:58:42

回答

1

使用這些方法列出每一行的最大/最小和列:

public void getMaximumOfEveryColumn() 
{ 
    for (int i = 0; i < A.length; i++) 
    { 
     max = Integer.MIN_VALUE; 
     for (int j = 0; j < A [ i ].length; j++) 
      if (A [ j ] [ i ] > max) 
       max = A [ j ] [ i ]; 
     System.out.println("Maximum of column " + i + " = " + max); 
    } 
} 

public void getMinimumOfEveryColumn() 
{ 
    for (int i = 0; i < A.length; i++) 
    { 
     min = Integer.MAX_VALUE; 
     for (int j = 0; j < A [ i ].length; j++) 
      if (A [ j ] [ i ] < min) 
       min = A [ j ] [ i ]; 
     System.out.println("Minimum of column " + i + " = " + min); 
    } 
} 
public void getMaximumOfEveryRow() 
{ 
    for (int i = 0; i < A.length; i++) 
    { 
     max = Integer.MIN_VALUE; 
     for (int j = 0; j < A [ i ].length; j++) 
      if (A [ i ] [ j ] > max) 
       max = A [ i ] [ j ]; 
     System.out.println("Maximum of row " + i + " = " + max); 
    } 
} 

public void getMinimumOfEveryRow() 
{ 
    for (int i = 0; i < A.length; i++) 
    { 
     min = Integer.MAX_VALUE; 
     for (int j = 0; j < A [ i ].length; j++) 
      if (A [ i ] [ j ] < min) 
       min = A [ i ] [ j ]; 
     System.out.println("Minimum of row " + i + " = " + min); 
    } 
} 
+0

非常感謝。 :) – 2012-02-23 00:34:42

+0

嘿,我得到了一些代碼,但你介意請解釋一下。 – 2012-02-23 23:13:05

+0

上述解決方案不適用於非方矩陣。你能否看清楚我的答案並糾正它。 – 2015-04-20 09:36:27

2

這在同一行上獲得最低限度的值作爲最大

public int minOnMaxRow(){ 

    int max = Integer.Min_Value(); 
    int min; 

    for (int i = 0; i < A.length; i++){ 
     for (int j = 0; j < A[i].length; j++){ 
      if (A[i][j] > max){ 
       max = A[i][j]; 
       min = Integer.Max_Value(); 
       for(k= 0; k < A[i].length; k++){ 
       if (A[i][k] < min){ 
        min = A[i][k] 
        } 
       } 
      } 
     } 
    } 
    return min; 
} 
+0

Integer.Min_Value()是做什麼的? – 2012-02-22 20:28:43

+1

這是Integer類中的一個靜態方法,它返回整數可以採用的最小值,Max_Value做相反 – nist 2012-02-22 20:48:43

+0

非常感謝。 :) – 2012-02-23 00:35:25

0

矩陣中列的最小值和最大值: -

import java.util.Scanner; 

public class Max_Min_Row_Col { 

    static int col; 
    static int row; 
    static int[][] trans_arr = new int[col][row]; 

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

     Scanner sc = new Scanner(System.in); 
     int m = sc.nextInt(); 
     row = m; 
     int n = sc.nextInt(); 
     col = n; 
     int[][] arr = new int[row][col]; 

     for (int i = 0; i < row; i++) { 
      for (int j = 0; j < col; j++) { 
       arr[i][j] = sc.nextInt(); 
       System.out.print(arr[i][j] + " "); 
      } 
      System.out.println(); 
     } 

     int i_old = 0; 
     int m_old = arr[i_old].length; 

     for (int i = 0; i < m_old; i++) { 
      int min = Integer.MAX_VALUE; 
      for (int j = 0; j < arr.length; j++) 
       if (arr[j][i] < min) 
        min = arr[j][i]; 
      System.out.println("Minimum of column " + i + " = " + min); 
     } 

     for (int i = 0; i < m_old; i++) { 
      int max = Integer.MIN_VALUE; 
      for (int j = 0; j < arr.length; j++) 
       if (arr[j][i] > max) 
        max = arr[j][i]; 
      System.out.println("Maximum of column " + i + " = " + max); 
     } 
    } 

} 
相關問題