我试图找出给定数组的最大和最近的子矩阵平方,但是我的代码在这里仅打印出最大的值。
我认为我的问题是,当代码找到最大值时,该值已经与以前找到的最大值相同,它需要传递并寻找更大的值,但可以保存以后出现的值的位置。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int numofrow = sc.nextInt();
int[][] a = new int[numofrow][numofrow];
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a.length; j++)
a[i][j] = sc.nextInt();
int[] b = biggest(a);
if (b != null) {
System.out.println(b[0] + " " + b[1] + " " + b[2]);
}
}
public static int[] biggest(int[][] a) {
int[] biggestsqaure = null;
int biggestsize = 0;
for (int k = 0; k < a.length; k++) {
for (int l = 0; l < a.length; l++) {
int count = getcount(a, k, l);
if (count < 2)
continue;
if ((count > biggestsize) && possiblesquare(a, k, l, count)) {
biggestsize = count;
biggestsqaure = new int[]{k, l, count};
}
}
}
return biggestsqaure;
}
public static int getcount(int[][] a, int row, int column) {
int count = 0;
if (column == a[0].length - 1)
return 0;
for (int m = column; m < a[0].length; m++) {
if (a[row][m] == 1) {
count++;
if (m == a[0].length - 1)
return count;
}
else if (count > 1)
return count;
else
return 0;
}
return 0;
}
public static boolean possiblesquare(int[][] a, int row, int column, int count) {
if (row + count > a.length)
return false;
for (int n = row; n < count + row; n++) {
for (int o = column; o < count + column; o++) {
if (a[n][o] == 0)
return false;
}
}
return true;
}
}