Home  >  Article  >  Backend Development  >  PHP prints ring number circle_PHP tutorial

PHP prints ring number circle_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:42:49880browse

As in the title:
$i=[number];
Print the following graphic:

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"> * Two-dimensional array sorting </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"> * Generate a ring of numbers </span> </span></li>
<li class="alt"><span><span class="comment"> * @param array $cotainer Two-dimensional array, Contains the generated circle </span> </span></li>
<li><span><span class="comment"> * @param int $start_value The starting value of the circle </span> </span></li>
<li class="alt"><span><span class="comment"> * @param int $x The offset value of the circle </span> </span></li>
<li><span><span class="comment"> * @param int $grids_edge The number of numbers on each 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><span class="vars">$grids_edge</span><span>;</span><span class="vars">$i</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 /***Two-dimensional array sorting *@paramarray$array*/ function sort_recursive( $array ){ foreach ( $array...</span>
</div>
<div class="art_confoot"></div></span></span></li>
</ol>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn