首页 > 后端开发 > php教程 > PHP打印环状数字圈_PHP教程

PHP打印环状数字圈_PHP教程

WBOY
发布: 2016-07-13 17:42:49
原创
902 人浏览过

如题:
$i=[number];
打印如下的图形:

1  2  3  4
12 13 14 5
11 16 15 6
10 9  8  7

 

<ol class="dp-c">
<li class="alt"><span><span class="comment">/**  </span> </span></li>
<li><span><span class="comment"> * 二维数组排序  </span> </span></li>
<li class="alt"><span><span class="comment"> * @param   array $array  </span> </span></li>
<li><span><span class="comment"> */</span><span>    </span></span></li>
<li class="alt">
<span class="keyword">function</span><span> sort_recursive(&</span><span class="vars">$array</span><span>){    </span>
</li>
<li>
<span>    </span><span class="keyword">foreach</span><span>(</span><span class="vars">$array</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$key</span><span>=></span><span class="vars">$edge</span><span>){    </span>
</li>
<li class="alt">
<span>        ksort(</span><span class="vars">$edge</span><span>,true);    </span>
</li>
<li>
<span>        </span><span class="vars">$array</span><span>[</span><span class="vars">$key</span><span>]=</span><span class="vars">$edge</span><span>;    </span>
</li>
<li class="alt"><span>    }    </span></li>
<li>
<span>    ksort(</span><span class="vars">$array</span><span>,true);    </span>
</li>
<li class="alt"><span>}    </span></li>
<li><span>    </span></li>
<li class="alt">
<span class="comment">/**  </span> </li>
<li><span><span class="comment"> * 生成一圈环状数字  </span> </span></li>
<li class="alt"><span><span class="comment"> * @param   array $cotainer    二维数组,包含生成的圈  </span> </span></li>
<li><span><span class="comment"> * @param   int $start_value    该圈的起始值  </span> </span></li>
<li class="alt"><span><span class="comment"> * @param   int $x    该圈的偏移值  </span> </span></li>
<li><span><span class="comment"> * @param   int $grids_edge    每条边的数字个数  </span> </span></li>
<li class="alt"><span><span class="comment"> */</span><span>    </span></span></li>
<li>
<span class="keyword">function</span><span> generate(&</span><span class="vars">$container</span><span>,</span><span class="vars">$start_value</span><span>,</span><span class="vars">$x</span><span>,</span><span class="vars">$grids_edge</span><span>){    </span>
</li>
<li class="alt">
<span>    </span><span class="vars">$rec_times</span><span>=</span><span class="func">ceil</span><span>(</span><span class="vars">$grids_edge</span><span>/2);    </span>
</li>
<li>
<span>    </span><span class="vars">$center</span><span>=</span><span class="vars">$grids_edge</span><span>&1?false:</span><span class="vars">$start_value</span><span>+4*</span><span class="vars">$rec_times</span><span>*(</span><span class="vars">$rec_times</span><span>+1);    </span>
</li>
<li class="alt">
<span>    </span><span class="keyword">if</span><span>(</span><span class="vars">$center</span><span>){    </span>
</li>
<li>
<span>        </span><span class="vars">$index</span><span>=</span><span class="vars">$x</span><span>+</span><span class="func">ceil</span><span>(</span><span class="vars">$grids_edge</span><span>/2);    </span>
</li>
<li class="alt">
<span>        </span><span class="vars">$container</span><span>[</span><span class="vars">$index</span><span>][</span><span class="vars">$index</span><span>]=</span><span class="vars">$center</span><span>;    </span>
</li>
<li><span>    }    </span></li>
<li class="alt">
<span>    </span><span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><span class="vars">$grids_edge</span><span>;</span><span class="vars">$i</span><span>++){    </span></span>
</li>
<li>
<span>        </span><span class="vars">$right</span><span>=</span><span class="vars">$x</span><span>+</span><span class="vars">$grids_edge</span><span>;    </span>
</li>
<li class="alt">
<span>        </span><span class="vars">$basevalue</span><span>=</span><span class="vars">$start_value</span><span>+</span><span class="vars">$i</span><span>;    </span>
</li>
<li>
<span>        </span><span class="vars">$container</span><span>[</span><span class="vars">$x</span><span>][</span><span class="vars">$x</span><span>+</span><span class="vars">$i</span><span>]=</span><span class="vars">$base_value</span><span>;    </span>
</li>
<li class="alt">
<span>        </span><span class="vars">$container</span><span>[</span><span class="vars">$x</span><span>+</span><span class="vars">$i</span><span>][</span><span class="vars">$right</span><span>]=</span><span class="vars">$grids_edge</span><span>+</span><span class="vars">$base_value</span><span>;    </span>
</li>
<li>
<span>        </span><span class="vars">$container</span><span>[</span><span class="vars">$right</span><span>][</span><span class="vars">$right</span><span>-</span><span class="vars">$i</span><span>]=</span><span class="vars">$grids_edge</span><span>*2+</span><span class="vars">$base_value</span><span>;    </span>
</li>
<li class="alt">
<span>        </span><span class="vars">$container</span><span>[</span><span class="vars">$right</span><span>-</span><span class="vars">$i</span><span>][</span><span class="vars">$x</span><span>]=</span><span class="vars">$grids_edge</span><span>*3+</span><span class="vars">$base_value</span><span>;    </span>
</li>
<li><span>    }    </span></li>
<li class="alt"><span>}    </span></li>
<li><span>    </span></li>
<li class="alt"><span>    </span></li>
<li>
<span class="vars">$container</span><span>=</span><span class="keyword">array</span><span>();    </span>
</li>
<li class="alt">
<span class="vars">$data</span><span>=</span><span class="keyword">array</span><span>(    </span>
</li>
<li>
<span>    1 =></span><span class="keyword">array</span><span>(1,6),    </span>
</li>
<li class="alt">
<span>    25=></span><span class="keyword">array</span><span>(2,4),    </span>
</li>
<li>
<span>    41=></span><span class="keyword">array</span><span>(3,2),    </span>
</li>
<li class="alt"><span>);    </span></li>
<li>
<span class="comment">/*  </span> </li>
<li class="alt"><span><span class="comment">$data=array(  </span> </span></li>
<li><span><span class="comment">    1 =>array(1,2),  </span> </span></li>
<li class="alt"><span><span class="comment">);  </span> </span></li>
<li><span><span class="comment">$data=array(  </span> </span></li>
<li class="alt"><span><span class="comment">    1&						
<p align="left"></p>
<div style="display:none;">
<span id="url" itemprop="url">http://www.bkjia.com/PHPjc/486031.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/486031.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">如题: $i=[number]; 打印如下的图形: 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 /** *二维数组排序 *@paramarray$array */ function sort_recursive( $array ){ foreach ( $array...</span>
</div>
<div class="art_confoot"></div>
</span></span></li>
</ol>
登录后复制
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板