How can I print integer in triangle form

前端 未结 4 2305
时光取名叫无心
时光取名叫无心 2021-02-13 22:20

I want to print integer in triangle form which look like this

    1
   121
  12321

I tried this but I do not get the actual result



        
相关标签:
4条回答
  • 2021-02-13 22:36

    Another integer solution:

    $n = 9; 
    print str_pad ("✭",$n," ",STR_PAD_LEFT) . PHP_EOL;
    for ($i=0; $i<$n; $i++){
        print str_pad ("", $n - $i);
        for ($ii=-$i; $ii<=$i; $ii++){
          if ($i % 2 != 0 && $ii % 2 == 0)
            print "&#" . rand(10025,10059) . ";";
          else print $i - abs($ii) + 1;
        }
        print PHP_EOL;
    }
    
            ✭
            1 
           1✬1 
          12321 
         1❊3✪3✳1 
        123454321 
       1✼3✶5❃5❈3✸1
      1234567654321 
     1✾3✯5✿7❉7✫5✷3✶1 
    12345678987654321 
    

    Or if you already have the string, you could do:

    $n = 9; $s = "12345678987654321"; $i = 1;
    
    while ($i <= $n)
       echo str_pad ("", $n-$i) . substr ($s,0,$i - 1) . substr ($s,-$i++) . PHP_EOL;
    
    0 讨论(0)
  • 2021-02-13 22:38

    Your code should be this:

    for($i=1;$i<=3;$i++)
     {
        for($j=3;$j>$i;$j--)
        {
          echo "&nbsp;&nbsp;";
        }
       for($k=1;$k<$i;$k++) /** removed = sign*/
        {
          echo $k;
        }
       if($i>=1) /**added = sign*/
        {
          for($m=$i; $m>=1; $m--)
             {
               echo $m;
             }
         }      
        echo "<br>";
    }
    

    Try this.

    Details:

    1. Your loop is not proper as in case of for($k=1;$k<=$i;$k++), this will print the repeated number when check the condition for less then and again for equals to. So remove the equals sign.

    2. reason to add the eqaul sign in if($i>=1) is that the first element will not print if there will not be equals as first it will be print by for loop from where removed the equal sign.

    Your output will be this:

       1
      121
     12321
    
    0 讨论(0)
  • 2021-02-13 22:55

    For all the x-mas lovers:

    $max = 9; # can be 2 .. 9
    for($i = 1; $i <= $max; $i++) {
        $line = (str_pad('', $max - $i));
        for($ii = 1; $ii <= $i; $ii++) {
            $line .= $ii;
        }   
        for($ii = $i-1; $ii > 0; $ii--) {
            $line .= $ii;
        }   
        echo $line . PHP_EOL;
    }
    

    Output:

            1
           121
          12321
         1234321
        123454321
       12345654321
      1234567654321
     123456787654321
    12345678987654321
    

    Amazing what computers are able to achieve nowadays! Isn't it?

    0 讨论(0)
  • 2021-02-13 22:56

    A little late to the party, but here's yet another solution that uses a "for" loop with two initialization variables and a ternary-based incrementer/decrementer. It's an unorthodox use of a "for" loop, but it's still perfectly valid and arguably makes the code more elegant and easier to follow. I chose to add space before and after each semicolon and omit all other space inside the parentheses so it's easier to visualize each of the three pieces of the "for" loop (initialization, condition, increment/decrement):

    $count = 9;
    echo "<pre>";
    for ($i=1; $i<=$count; $i++) {
        echo str_pad("",$count-$i," ",STR_PAD_LEFT);
        for ( $j=1,$up=true ; $j>0 ; $up?$j++:$j-- ) {
            echo $j;
            if ($j==$i) {$up = false;}
        }
        echo "<br>";
    }
    echo "</pre>";
    

    Output:

            1
           121
          12321
         1234321
        123454321
       12345654321
      1234567654321
     123456787654321
    12345678987654321
    
    0 讨论(0)
提交回复
热议问题