gpt4 book ai didi

php - 如何使用 php 创建金字塔?

转载 作者:可可西里 更新时间:2023-10-31 23:52:39 24 4
gpt4 key购买 nike

我需要使用星号创建一个金字塔。我指定一个值,它成为金字塔的基础。基数包含与指定值一样多的星号,金字塔必须跳过其行 1..这里我在指定偶数个基数时遇到问题..

金字塔必须看起来像下面的那个。

    * 
***
*****
*******
*********
**********

我得到了

####* 
###***
##*****
###*****
####*****
**********

我想用一些空格替换 #,但我收到第 4 行中星号数量减少的错误。我该如何修复这两个错误?

function create_pyramid($limit){

if ($limit > 0){
for ($row =0;$row<=$limit;$row++){
if (($row % 2 == 0) && ($row != $limit)){ continue;}
$rows = "";
for ($col =0;$col<$row;$col++){
$rows= $rows.'*';
}
$pattern = "%'#".((($limit - $row)/2)+$row)."s\n";
printf ($pattern,$rows);
print '<br />';
}
}
else{
print "Invalid data";
}
}

create_pyramid(10);

最佳答案

我更喜欢我的:

  echo '<pre>';

$n = 5;
function print_tree($n, $str, $max) {
for ($i = 0; ($i < (($max - $n) / 2)); $i++) {
echo "&nbsp;";
}
for ($i = 0; ($i < $n); $i++) {
echo $str;
}
echo "<br/>";
}

for ($flag = 0; ($flag < 2); $flag++) {
for ($a = 1, $b = 1, $c = 1, $d = 4; (($d - 3) <= $n); $a += 2, $b++) {
if ($flag == 1) {
print_tree($a, "*", $max);
}
if ($b == $d) {
if ($flag == 0) {
$max = $a;
}
if (($d - 3) != $n) {
$a -= ((2 * $c) + 2);
}
$b = 0;
$d++;
if (($d % 2) == 0) {
$c++;
}
}
}
}
if ((($foot = $n) % 2) == 0) {
$foot++;
}
for ($i = 0; ($i < $foot); $i++) {
print_tree($foot, "|", $max);
}

输出:

                   *
***
*****
*******
*****
*******
*********
***********
*************
***********
*************
***************
*****************
*******************
*********************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
*************************
***************************
*****************************
*******************************
*********************************
***********************************
*************************************
***************************************
|||||
|||||
|||||
|||||
|||||

甚至这个:

<?php

$n = 8;

ob_start();

$stars = ($n - 1) * 2 + 1;
$spaces = 0;
for ($i = 0; ($i < $n); $i++) {
echo str_repeat(' ', $spaces);
echo str_repeat('*', $stars);
echo ' ';
echo str_repeat(' ', $spaces * 2);
echo str_repeat('*', $stars);
echo "\n";
$spaces += 1;
$stars -= 2;
}

$stars = ($n - 1) * 2 + 1;
$spaces = 0;
$margin = $stars / 2 + 1;
for ($i = 0; ($i < $n); $i++) {
echo str_repeat(' ', $margin);
echo str_repeat(' ', $spaces);
echo str_repeat('*', $stars);
echo "\n";
$spaces += 1;
$stars -= 2;
}

echo trim(implode("\n", array_reverse(explode("\n", ob_get_clean()))), "\n"), "\n";

它给出:

               *
***
*****
*******
*********
***********
*************
***************
* *
*** ***
***** *****
******* *******
********* *********
*********** ***********
************* *************
*************** ***************

有趣的练习不是吗... 8-)

关于php - 如何使用 php 创建金字塔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12799918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com