***8.35(最大块)给定一个元素为0或者1的方阵,编写程序,找到一个元素都为1的最大的子方阵。程序提示用户输入矩阵的行数。然后显示最大的子方阵的第一个元素、行数
题目
题目描述与运行示例
***8.35(最大块)给定一个元素为0或者1的方阵,编写程序,找到一个元素都为1的最大的子方阵。程序提示用户输入矩阵的行数。然后显示最大的子方阵的第一个元素、最大子方阵的行数
Enter the number of rows in the matrix: 5
Enter the matrix row by row:
1 0 1 0 1
1 1 1 0 1
1 0 1 1 1
1 0 1 1 1
1 0 1 1 1
The maximum square submatrix is at (2, 2) with size 3
破题
- 输出提示语句,从控制台获取方阵大小
- 声明一个二维数组,长度为刚刚获取的大小
- 使用循环获取用户赋值
- 使用多层循环遍历数组,找到最大的子方阵
- 输出子方阵第一个元素位置和行数
代码(非正式)
import java.util.Scanner;
public class Test8_35 {
public static void main(String[] args) {
//1. 输出提示语句,从控制台获取方阵大小
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows in the matrix: ");
int length = input.nextInt();
//2. 声明一个二维数组,长度为刚刚获取的大小
int[][] arr = new int[length][length];
//3. 使用循环获取用户赋值
System.out.println("Enter the matrix row by row: ");
for (int i = 0