/*问题描述
找出下面给出图形的规律,给出n,画一个n行的对应图形。
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
输入格式
输入包含一个数n。1<=n<=30
输出格式
输出与上图类似n行的图形。
样例输入
4
样例输出
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7*/
import java.util.*;
public class Main {
public static int n;
public static int[][] array;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
array = new int[n][n];
int i = 0, j = -1, result = 0;
while (result < n * n) {
// System.out.println(result);
while (j + 1 < n && array[i][j + 1] == 0) {
array[i][++j] = ++result;
}
while (i + 1 < n && array[i + 1][j] == 0) {
array[++i][j] = ++result;
}
while (j - 1 >= 0 && array[i][j - 1] == 0) {
array[i][--j] = ++result;
}
while (i - 1 > 0 && array[i - 1][j] == 0) {
array[--i][j] = ++result;
}
}
sc.close();
for (int k = 0; k < n; k++) {
for (int m = 0; m < n; m++) {
System.out.print(array[k][m] + " ");
}
System.out.println();
}
}
}
绘制图形
最新推荐文章于 2022-09-06 10:46:25 发布