2012-10-09 89 views
3

我需要用星号创建一个金字塔。我指定一个成为金字塔基础的值。该基地包含尽可能多的星号作为指定的值,金字塔必须跳过它的行1 ..在我指出一个问题时,我指定偶数个基地..如何使用php创建金字塔?

金字塔必须像下面的一个looke。

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

我越来越

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

我想通过一些空格来代替#和我得到的是星号的第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); 
+5

仅供参考,阿斯特里克斯是一个卡通人物。 –

+0

不相关,但我希望它有帮助:删除'if(($ row%2 == 0)&&($ row!= $ limit)){continue;}'并替换for for循环for($ row = 0 ; $ row <= $ limit; $ row + = 2){ ' – RRikesh

回答

6

只是使其更简单:

function create_pyramid($limit) { 
    for($row = 1; $row < $limit; $row ++) { 
     $stars = str_repeat('*', ($row - 1) * 2 + 1); 
     $space = str_repeat(' ', $limit - $row); 
     echo $space . $stars . '<br/>'; 
    } 
} 
echo "<pre>" ; 
create_pyramid(10); 
+1

测试你的代码..它的结构不一样..它对齐左边http://codepad.viper-7.com/2KYVOl – Baba

+0

不,它是不。该网站是错误的。将''更改为'#'并再次测试。 – Nin

+0

该网站没有错..现在检查它.. http://codepad.viper-7.com/wwzaqg – Baba

5

您可以尝试

create_pyramid("*", 5); 
create_pyramid("@", 10); 
create_pyramid("^_^", 10); 

function create_pyramid($string, $level) { 
    echo "<pre>"; 
    $level = $level * 2; 
    for($i = 1; $i <= $level; $i ++) { 
     if (!($i % 2) && $i != 1) 
      continue; 
     print str_pad(str_repeat($string, $i),($level - 1) * strlen($string), " " , STR_PAD_BOTH); 
     print PHP_EOL; 
    } 
} 

输出A

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

输出B

  @   
     @@@   
     @@@@@  
     @@@@@@@  
    @@@@@@@@@  
    @@@@@@@@@@@  
    @@@@@@@@@@@@@ 
    @@@@@@@@@@@@@@@ 
@@@@@@@@@@@@@@@@@ 
@@@@@@@@@@@@@@@@@@@ 

输出C

     ^_^^_^^_^       
        ^_^^_^^_^^_^^_^      
        ^_^^_^^_^^_^^_^^_^^_^     
       ^_^^_^^_^^_^^_^^_^^_^^_^^_^    
      ^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^    
     ^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^   
     ^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^  
    ^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^ 
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^ 
8

我更喜欢矿:

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"; 

它给:

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

搞笑exercices是不是... 8-)

+3

+1尼斯..圣诞节快乐.... @Ninsuo – Baba

1
<?php 

$n=9; 
for($i=0; $i<=$n; $i++) 
{ 

for($j=1; $j<=$i; $j++) 

       echo "&nbsp;"; 


    for($k=1; $k<=$n-$i; $k++) 

     echo $k; 


     for($j=($k-2); $j>0; $j--) 

        echo $j; 





      for($k=1; $k<=$i; $k++) 

         echo "&nbsp;"; 






    echo "</br>"; 

} 





?> 
+2

输出完全像你的代码格式 – Baba

0
function create_row($num, $limit) { 

    $append = ''; 

    if($limit > $num && ($limit - $i) % 2 == 0) { 

     $append .= '-'; 

    } 

    $stars = str_repeat('*', $num); 

    $ap_len = floor(($limit - $num)/2); 

    $prepend = str_repeat('-', $ap_len); 

    $append .= str_repeat('-', $ap_len); 

    return $prepend . $stars . $append; 

} 

function create_pyramid($limit){ 

    if ($limit > 0){ 

     $no_last = false; 

     for($i = 1; $i <= $limit; $i += 2) { 

      print create_row($i, $limit) . PHP_EOL; 

      if($i == $limit) { 

       $no_last = true; 

      } 

     } 

     if(!$no_last) { 

      print create_row($limit, $limit) . PHP_EOL; 

     } 

    } 

} 

create_pyramid(10); 
0

据我所知,你正在寻找的是一个奇数金字塔,即每行中*的数量按照奇数序列,如1,3,5,7。如果您希望包括“的if-else”语句的话,你可以用上面的回答圈走了,但如果你只是想使用“for”循环,那么你可以使用下面的代码:

<?php 
$x=1; 
    for($i=1;$i<7;$i++) 
     { 
     for($j=7;$j>$i;$j--) 
      { 
      echo '&nbsp;&nbsp;'; 
      } 
     for($k=1;$k<=$x;$k++) 
      { 
      echo '*'; 
      } 
      $x=$x+2; 
      echo "<br/>"; 
     } 
?> 
0

我想最简单的解决方案是创建2个回路与条件:

$n = 5; // specify how many rows you want to 
$stars = 0; 

for ($i = $n; $i > 0; $i--) { 
    for ($j = 0; $j < $i + $stars; $j++) { 
     if ($j < $i - 1) { 
      echo " "; 
     } else { 
      echo "*"; 
     } 
    } 

    $stars += 2; 
    echo "\n"; 
} 

的输出将是:

* 
    *** 
    ***** 
******* 
********* 
0
<?php 
$l=5; 
for ($i=1; $i <=5; $i++) { 

    for ($k=1; $k < $l ; $k++) { 

     echo '&nbsp;'; 

    } 

    for ($j=0; $j<$i; $j++) { 



     echo '*'; 
    } 

    $l--; 

    echo "<br>"; 

} 

?> 
0
function create_piramide ($filas) {  
    $n = $filas; 
    echo '<pre>'; 
    for ($i = $filas; $i >= 0; $i--) {    
     echo str_repeat(' ', $i).str_repeat('o ', $n - $i)."\n"; 
    } 
    echo '</pre>'; 
} 

create_piramide (10); 
0

试试这个。

  $num = 5; 
      for($row = 1; $row <= $num; $row++){ 
       for($colspace = 1; $colspace <= $num-$row; $colspace++){ 
        echo "&nbsp;"; 
       } 
       for($col = 1; $col <= $row; $col++){ 
        echo "* "; 
       } 
       echo "<br />"; 
      } 
+0

为什么?请添加解释和推理。只有代码答案不是很有用。 – Martin

0
function pyramid($height) 
{ 

    for ($i = 1; $i <= $height; $i++) { 
     echo str_repeat("&nbsp;&nbsp;", $height - $i); 
     echo str_repeat('*', $i * 2 - 1) . '<br/>'; 
    } 

} 


pyramid(5); 
+0

尽管此代码可能会回答这个问题,但提供关于此代码为何和/或如何回答此问题的其他上下文可提高其长期价值。不鼓励使用仅有代码的答案。 – Ajean

0

钻石形状

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



<?php 
     $num=15; 
     $num2=0; 
     $half = $num; 
      for($i=$num2; $i<=$num; $i++) 
      {  
       for($j=$half; $j>$i; $j--) 
       { 
        echo "&nbsp;&nbsp;";  
       } 
       for($j=$num2; $j<=$i; $j++) 
       { 
        echo "&nbsp;*&nbsp;"; 
       } 
       echo "<br>"; 
      } 

      for($i=$num2; $i<=$num; $i++) 
      {  
       for($j=$num2; $j<=$i; $j++) 
       { 
        echo "&nbsp;&nbsp;";  
       } 
       for($j=$half; $j>$i; $j--) 
       { 
        echo "&nbsp;*&nbsp;"; 
       } 
       echo "<br>&nbsp;"; 

      } 

    ?> 
+1

仅有代码的答案对未来的读者没有帮助。 [编辑]答案并添加解释。 – Tushar

-1
<?php 

ini_set('display_errors', 1); 

/** 
* Here is my solution for printing the pyramid using a class 
*/ 

class pyramid 
{ 
    function __construct($rows) 
    { 

     for ($k=1; $k <= $rows ; $k++) { 
      $this->space($rows,$k); 
      $this->left($k); 
      $this->right($k); 
      echo "<br>"; 
     } 
    } 


    public function left($k) 
    { 
     for ($i=1; $i < $k ; $i++) { 
      echo $i; 
     } 
    } 
    public function right($i) 
    { 
     for ($j=$i; $j >= 1 ; $j--) { 
      echo $j; 
     } 
    } 
    public function space($rows,$k) 
    { 
     for ($i=$rows-$k; $i > 0 ; $i--) { 
      echo "&nbsp;"; 
     } 
    } 
} 

$pyramid = new pyramid(5); 

?> 
<style type="text/css"> 
    body{ 
     font-family: monospace; 
     font-size: 24px; 
    } 
</style> 
+0

请考虑包括你的答案的描述。 –

0
$n = 5; 
for($i = $n; $i >= 1; $i--){ 
    for($j = 1; $j <= $i; $j++){ 
     if($j < $i){ 
      echo '0';//you can replace 0 with space 
     }else{ 
      $num = $n - $i; 
      for($k = 0; $k <= $num; $k++){ 
       echo '*'; 
      } 
      for($y = 1; $y <= $n - 1; $y++){ 
       if($y <= $num){ 
        echo '*'; 
       }else{ 
        echo '0';//you can replace 0 with space 
        if($y == $n - 1){ 
         echo '<br/>'; 
        } 
       } 
      } 
     } 
    } 
}