public class Main
{
public static void main
(String[] args
) {
int w = 5, h = 7;
int[][] arr = new int[w][h];
int count = 1;
for (int i = 0; count <= w*h; i++)
{
for (int x = i; x < w-i && count <= w*h; x++)
arr[x][i] = count++;
for (int y = i+1; y < h-i && count <= w*h; y++)
arr[w-i-1][y] = count++;
for (int x = w-2-i; x >= i && count <= w*h; x--)
arr[x][h-i-1] = count++;
for (int y = h-2-i; y > i && count <= w*h; y--)
arr[i][y] = count++;
}
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
System.
out.
printf("%3d",arr
[x
][y
]); }
}
}
cHVibGljIGNsYXNzIE1haW4KewogICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKQogICB7CiAgICAgIGludCB3ID0gNSwgaCA9IDc7CiAgICAgIGludFtdW10gYXJyID0gbmV3IGludFt3XVtoXTsKICAgICAgaW50IGNvdW50ID0gMTsKICAgICAgZm9yIChpbnQgaSA9IDA7IGNvdW50IDw9IHcqaDsgaSsrKQogICAgICB7CiAgICAgICAgIGZvciAoaW50IHggPSBpOyB4IDwgdy1pICYmIGNvdW50IDw9IHcqaDsgeCsrKQogICAgICAgICAgICBhcnJbeF1baV0gPSBjb3VudCsrOwogICAgICAgICBmb3IgKGludCB5ID0gaSsxOyB5IDwgaC1pICYmIGNvdW50IDw9IHcqaDsgeSsrKQogICAgICAgICAgICBhcnJbdy1pLTFdW3ldID0gY291bnQrKzsKICAgICAgICAgZm9yIChpbnQgeCA9IHctMi1pOyB4ID49IGkgJiYgY291bnQgPD0gdypoOyB4LS0pCiAgICAgICAgICAgIGFyclt4XVtoLWktMV0gPSBjb3VudCsrOwogICAgICAgICBmb3IgKGludCB5ID0gaC0yLWk7IHkgPiBpICYmIGNvdW50IDw9IHcqaDsgeS0tKQogICAgICAgICAgICBhcnJbaV1beV0gPSBjb3VudCsrOwogICAgICB9CiAgICAgIGZvciAoaW50IHkgPSAwOyB5IDwgaDsgeSsrKQogICAgICB7CiAgICAgICAgIGZvciAoaW50IHggPSAwOyB4IDwgdzsgeCsrKQogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50ZigiJTNkIixhcnJbeF1beV0pOwogICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsKICAgICAgfQogICB9Cn0K