我正在尝试在2D数组(Java)中找到最小值。
我正在尝试使用for循环。我的目标是创建一个程序来获得矩阵的鞍点但我会一步一步走。我已经想出如何在2D数组的行中找到最大值。然后我想在与最大值相同的列中找到最小值。
这是我的代码:
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 :(得分: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;
}
答案 1 :(得分: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 );
}
}
答案 2 :(得分: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);
}
}
}