首頁  >  文章  >  後端開發  >  PHP打印环状数字圈_PHP教程

PHP打印环状数字圈_PHP教程

WBOY
WBOY原創
2016-07-13 17:42:49875瀏覽

如题:
$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>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn