Hackerrank 在 php 中绘制长度为 N 的楼梯

问题描述 投票:0回答:15

像这样画一个高度为 N 的楼梯:

     #
    ##
   ###
  ####
 #####
######

高度为 6 的楼梯,注意最后一行的空格应为零。

我的解决方案无法正常工作

function draw($size)
{
    for ($i = 1; $i <=$size ; $i++)
    {
        $spaces = $size-$i;
        while ($spaces)
        {
            echo " ";
            $spaces--;
        }
        $stairs = 0;
        while ($stairs < $i)
        {
            echo "#";
            $stairs++;
        }
        echo "<br/>";
    }
}
draw(6);
//output
#
##
###
####
#####
######

它没有打印空格,我尝试了

\n, PHP.EOL
仍然不起作用。有什么建议吗?

php pattern-matching
15个回答
3
投票

虽然其他解决方案都很好,但这也是我的代码。

$max=5;
for ( $i =1 ; $i<=$max;$i++) {
        for ( $space = 1; $space <= ($max-$i);$space++) {
                echo " ";
        }
        for ( $hash = 1; $hash <= $i;$hash ++ ) {
                echo "#";
        }
        echo "\n";
}

2
投票
//PHP

$n = 6;  // Number of rows.
for($i=1;$i<=$n;$i++){
    echo str_repeat(' ', $n-$i) . str_repeat('#', $i);
    echo '\n';
}

1
投票
for ($i=0; $i<$n; $i++){
    for ($j=0; $j<$n; $j++){          
        if($i+$j>$n-2){
            echo "#";
        } else {
           echo " ";
        }
        if($j==$n-1 && $i+$j<$n*2-2){ //The second part is to dont break the last line
            echo "\n";
        } 
    }  
}

0
投票
for(var i = 0; i < n; i++)
{
    var s = "";
    for(var j = 0; j < n; j++)
    {
        if(n - i - 2 < j)
        {
            s += "#";
        }
        else
        {
            s += " ";
        }
    }
    console.log(s);
}

0
投票

这是另一个解决方案:

$int = 7;

for($i = 1; $i<=$int; $i++){ 
    printf('%1$s%2$s%3$s',str_repeat(" ",$int-$i),str_repeat("#",$i),"\n");
}

来自 PHP 官方文档:

str_repeat


0
投票
$n = 6;
for ($i = 0; $i < $n; $i++) {
$pad = 1;
for ($space = 0; $space < $n-$i-1; $space++) {
    $pad++;
}
echo str_pad('#', $pad,' ',STR_PAD_LEFT);
for ($j = 0; $j < $i; $j++) {
    echo '#';
}
echo '<br>';
}

0
投票

我花了一段时间,但最后我设法按照 OFC(A. Sharma)的例子做到了。

<?php

$handle = fopen("php://stdin","r");

$n = intval(fgets($handle));


for ($rows = 0; $rows < $n; $rows++) {

    for ($columns = 0; $columns < $n - $rows - 1; $columns++) {

        echo " ";
    }

    for ($columns = 0; $columns < $rows + 1; $columns++) {

        echo "#";
    }

echo "\n";

}

?>

0
投票

使用 PHP 函数 range()str_repeat() 获得优雅的解决方案:

function staircase($n){
    foreach (range(1, $n) as $i) 
        print( str_repeat(' ',$n-$i).str_repeat('#',$i)."\n");
}

演示


0
投票
  1. 检查 n 是否在 0 到 101 之间 ( 0 < n <= 100)
  2. 循环遍历每一行 2.1 根据最后一项位置打印空格 2.2 打印项目
  3. 单独行

下面的代码解释了一切...

function staircase($n) {
    // check if n is between 0 and 101 (0 < n <=100)
    if( 0 < $n && $n > 100 ) {

    } else {
        // Loop through each row
        for($i = 1; $i <= $n; $i++) {
            // print spaces according to the last item position
            $si = 1;
            while( $si <= ($n - $i)){
                print(" ");
                $si++;
            }
            // print the items
            for($j = 1; $j <= $i; $j++) {
                print("#");
            }
            // separate rows
            print("\n");
        }
    }
}

输出:对于 n = 6

     #
    ##
   ###
  ####
 #####
######

0
投票

在尝试了代码并尝试/失败了几次之后,我终于得到了正确的结果。请注意,为了打印空格和新行,我如何使用“ ”。之前的“
”和“ ”表示空间不起作用。

断行将在每个行号的循环中出现。因此,如果我们有 $n=4 ,那么换行符后的每 4 个空格将被回显。

我制作了 2 个循环来填充楼梯中的所有字段。 这里棘手的部分是让它们正确对齐。这就是 if 语句出现的地方。

参考链接: 黑客排名挑战

// Complete the staircase function below.
function staircase($n) {

   for($i=1; $i<=$n; $i++){
    
    for($j=1; $j <= $n; $j++){
        if( ($n - $i) < $j ){
            echo "#";
        }else{
            echo " ";
            
        }
     }
     echo "\n";
    }
  }

0
投票

试试这个

$n=6;
for($i=1;$i<=$n;$i++){
    
    for($spaces=1;$spaces<=($n-$i);$spaces++){
        echo " ";
    }
    for($staires=0;$staires<$i;$staires++){
        echo "#";
    }        
   echo "\n";
}

0
投票

这对我有用

$n = 6;
function staircase($n) {
  for($i=1; $i <= $n; $i++){
    for($j=1; $j <= $n; $j++){
      if($j > $n-$i){
        echo "#";  
      }else{
        echo " ";
      }    
    }
    echo "\n";
    }
}

0
投票
<?php
    
    
    class StairsOrPyramid{
        
        const BLOCKS = 10;
        
        function leftAllignedPyramid(){
            
            for($i=0 ; $i < self::BLOCKS; $i++){
                for($j=0;$j <= $i;$j++){
                    print("*");
                }
                print("\n");
            }
        }
        
        function rightAllignedPyramid(){
            
            for($i=self::BLOCKS; $i<=self::BLOCKS; $i--){
                for($j=1; $j <= self::BLOCKS; $j++){
                    $j < $i ? print(" "):print("*");
                }
                print("\n");
            }
            
        }
        
    }
    
    
    $stairs = new StairsOrPyramid();
    $stairs->leftAllignedPyramid();
    
    $stairs->rightAllignedPyramid();

-1
投票

使用

print(' ')
,如果您想转到下一行,请输入
print(' ')."\n"


-1
投票

JavaScript:

解决方案:

function StairCase(n){
  let x = [];
  for(let i = 0; i<n; i++){
    while(x.length < n){
        x.push(" ");
    }
    x.shift();
    x.push("#");
    console.log(x.join(''));
}   }         //StairCase(6)
© www.soinside.com 2019 - 2024. All rights reserved.