Today, a face test, ask for high-finger teaching
The title requirement is to write a function, enter a number n, and then the function will output an n-row n-column array, the array pattern is as follows:
N=3
-------------------
1 2 3
8 9 4
7 6 5
N=4
-------------------
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
N=5
-------------------
1 2 3) 4 5
16 17 18) 19 6
15 24 25) 20 7
14 23 22) 21 8
13 12 11) 10 9
Well, that's roughly the way it is, sort of around the circle.
→→→→┐
┌→→┐↓
↑┌→↓↓
↑└←┘↓
└←←←┘
-------------------
Have the wood has the master to make? 0.0
------Solution--------------------
PHP Code
function Circle ($n) {$k = $n% 2; $ar = Array (); for ($i =0; $i < $n; $i + +) $ar [$i] = Array_fill (0, $n, 0); $p = 1; $x = 0; $y = 0; $m = 0; $len = $n * $n; while ($p < $len) {$x = $y = $m; while ($x < $n) $ar [$y] [$x + +] = $p + +; $x--; $y + +; while ($y < $n) $ar [$y ++][$x] = $p + +; $y--; $x--; while ($x >= $m) $ar [$y] [$x-] = $p + +; $x + +; $y--; $m + +; while ($y >= $m) $ar [$y--][$x] = $p + +; $n--; if ($k) $ar [$y +1][$x +1] = $p; $r = '
'; foreach ($ar as $v) $r. = '
| '. Join (' |
', $v). ' |
'; return $r. '
'; }for ($i =2; $i <30; $i + +) {$html. = Circle ($i); $html. = '; }?> !--? php echo $html?-->