题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
import java.util.Scanner;
class Main{
// 这是另外一种写法 不同于用方向数组matrix本身当作boolean[][] visited判断
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
int[][] matrix = new int[n][m];
int left = 0, right = m - 1, top = 0, bottom = n - 1;
int val = 1;
while (left < right && top < bottom) {
for (int i = left; i < right; i) matrix[top][i] = val;
for (int i = top; i < bottom; i) matrix[i][right] = val;
for (int i = right; i > left; i–) matrix[bottom][i] = val;
for (int i = bottom; i > top; i–) matrix[i][left] = val;
left;
right–;
top;
bottom–;
}
if (top == bottom) {
for (int i = left; i <= right; i) matrix[top][i] = val;
} else if (left == right) {
for (int i = top; i <= bottom; i) matrix[i][left] = val;
}
for (int[] array : matrix) {
for (int i : array) {
System.out.print(i);
System.out.print(' ');
}
System.out.println();
}
}
}