标签:
今天碰到一个比较有意思的问题, 就是把A到Y这25个字母以下面的形式输出出来
| A | B | C | D | E |
| P | Q | R | S | F |
| O | X | Y | T | G |
| N | W | V | U | H |
| M | L | K | J | I |
| 1 | 2 | 3 | 4 | 5 |
| 16 | 17 | 18 | 18 | 6 |
| 15 | 24 | 25 | 20 | 7 |
| 14 | 23 | 22 | 21 | 8 |
| 13 | 12 | 11 | 10 | 9 |
| 1 | 2 | 3 | 4 | 5 |
| 12 | 13 | 14 | 15 | 6 |
| 11 | 10 | 9 | 8 | 7 |
| 1 | 2 | 3 |
| 12 | 13 | 4 |
| 11 | 14 | 5 |
| 10 | 15 | 6 |
| 9 | 8 | 7 |

/**
* 根据传入的行数和列数生成螺旋数组
* @author chenqionghe
* @param int $row 行数
* @param int $col 列数
* @return array
*/
function rotationSort($row=5,$col=5)
{
$k=1;
$result = array();
$small = $col < $row ? $col : $row;
$count = ceil($small / 2);
for($i=0; $i<$count; $i++)
{
$maxRight = $col-1-$i;//右边最大坐标
$maxBottom = $row -1 -$i;//下面最大坐标
for($j=$i; $j<=$maxRight; $j++) //构造上边一条线 纵坐标最小,横坐标递增
{
$result[$i][$j] = $k++;
}
for($j=$i; $j<$maxBottom; $j++) //构造右边一条线 纵坐标递增,横坐标最大
{
$result[$j+1][$maxRight] = $k++;
}
for($j=$maxRight-1;$j>=$i; $j--) //构造下边一条线 纵坐标最大,横坐标递减
{
if($result[$maxBottom][$j]) break;
$result[$maxBottom][$j] = $k++;
}
for($j=$maxBottom-1;$j>$i;$j--) //构造左边一条线 纵坐标递减,横坐标最小
{
if($result[$j][$i]) break;
$result[$j][$i] = $k++;
}
}
return $result;
}
/**
* 以table格式输出数组
* @param $result 螺旋数组
* @param $row 行数
* @param $col 列数
*/
function printArray($result,$row,$col)
{
echo ‘<table border=1 style="width:500px;">‘;
for($i=0;$i<$row;$i++)
{
echo ‘<tr>‘;
for($j=0;$j<$col;$j++)
{
echo ‘<td style="padding: 50px;">‘.$result[$i][$j].‘</td>‘;
}
echo ‘<tr>‘;
}
echo ‘</table>‘;
}
$row = 5; $col = 5; $arr = rotationSort($row,$col); printArray($arr,$row,$col);

$arr = array(‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘N‘, ‘M‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘,);
<?php
/**
* 根据传入的行数和列数生成螺旋数组
* @author chenqionghe
* @param int $row 行数
* @param int $col 列数
* @return array
*/
function rotationSort($row=5,$col=5)
{
$k=1;
$result = array();
$small = $col < $row ? $col : $row;
$count = ceil($small / 2);
for($i=0; $i<$count; $i++)
{
$maxRight = $col-1-$i;//右边最大坐标
$maxBottom = $row -1 -$i;//下面最大坐标
for($j=$i; $j<=$maxRight; $j++) //构造上边一条线 纵坐标最小,横坐标递增
{
$result[$i][$j] = $k++;
}
for($j=$i; $j<$maxBottom; $j++) //构造右边一条线 纵坐标递增,横坐标最大
{
$result[$j+1][$maxRight] = $k++;
}
for($j=$maxRight-1;$j>=$i; $j--) //构造下边一条线 纵坐标最大,横坐标递减
{
if($result[$maxBottom][$j]) break;
$result[$maxBottom][$j] = $k++;
}
for($j=$maxBottom-1;$j>$i;$j--) //构造左边一条线 纵坐标递减,横坐标最小
{
if($result[$j][$i]) break;
$result[$j][$i] = $k++;
}
}
return $result;
}
/**
* 以table格式输出数组
* @param $rotationArr 要输出的内容
* @param $result 螺旋数组
* @param $row 行数
* @param $col 列数
*/
function printArray($rotationArr,$result,$row,$col)
{
echo ‘<table border=1 style="width:500px;">‘;
for($i=0;$i<$row;$i++)
{
echo ‘<tr>‘;
for($j=0;$j<$col;$j++)
{
//echo ‘<td style="padding: 50px;">‘.$result[$i][$j].‘</td>‘;
echo ‘<td style="padding: 50px;">‘.$rotationArr[$result[$i][$j]-1].‘</td>‘;
}
echo ‘<tr>‘;
}
echo ‘</table>‘;
}
$arr = array(‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘N‘, ‘M‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘,);
$row = 5;
$col = 5;
$rotationArr = rotationSort($row,$col);
printArray($arr,$rotationArr,$row,$col);

标签:
原文地址:http://www.cnblogs.com/chenqionghe/p/4765802.html