题目要求是写一个函数,输入一个数字n,然后函数就输出一个n行n列的数阵,数阵样式举例如下:n=3
-------------------
1 2 3
8 9 4
7 6 5n=4
-------------------
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7n=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嗯,大致就是这么着,绕着圈子排序
→→→→┐
┌→→┐↓
↑┌→↓↓
↑└←┘↓
└←←←┘-------------------
有木有高手做出来? 0.0
-------------------
1 2 3
8 9 4
7 6 5n=4
-------------------
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7n=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嗯,大致就是这么着,绕着圈子排序
→→→→┐
┌→→┐↓
↑┌→↓↓
↑└←┘↓
└←←←┘-------------------
有木有高手做出来? 0.0
$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 = '<table>';
foreach($ar as $v)
$r .= '<tr><td>' . join('</td><td>', $v) . '</td></tr>';
return $r.'</table>';
}
for($i=2;$i<30;$i++){
$html .= Circle( $i );
$html .='<hr>';
}
?>
<html>
<head>
<style>
ul li {
float: left;
width: 40px;
height: 40px;
}
hr{clear:both;}
</style>
</head>
<body>
<?php
echo $html?>
</body>
</html>