您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

顺时针方向打印矩阵解决思路

2024/2/23 4:56:53发布24次查看
顺时针方向打印矩阵
http://blog.csdn.net/wusuopubupt/article/details/12788249
在这里看到的。
/**
* @author:wusuopubupt
* @date:2013-10-16
* @from:http://ac.jobdu.com/problem.php?pid=1391
*
* print matrix in clockwise
* */
$matrix = array
(
array(1,2,3,4),
array(5,6,7,8),
array(9,10,11,12),
array(13,14,15,16),
array(17,18,19,20)
);
print_matrix($matrix);
function print_matrix($arr) {
$top = 0;
$left = 0;
$right = count($arr[0])-1;
$bottom = count($arr)-1;
while ($left != $right && $top != $bottom) {
//top
for($j = $left; $j = $top; $i--) {
echo $arr[$i][$left]. ;
}
$left++;
}
}
为啥输出结果是这样的呢?1 2 3 4 8 12 16 20 19 18 17 13 9 5 6 7  11 15 14 10
分享到: 更多
------解决方案--------------------
可能是因为每个数组的长度都相同才可以吧。 上下左右 不断的减1.
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product