1

我正在尝试垂直打印数字,它必须在组中

这是我的代码

$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
for ($g = 0; $g <= $group; $g++) {
    echo "<div class='group'>";
    for ($i = 1; $i <= $rows; $i++) {
        for ($j = $i; $j <= 24; $j = $j + $rows) {
            $count++;
            if($count>$nums){
                break;
        }
            echo "<div class='fleft'>$count</div>";
        }
        echo "<div class='clear'></div>";
    }
    echo "</div>";
}

从上面

在此处输入图像描述

但我想要第一列的输出

在此处输入图像描述

下一个组号将从第一个组号结束的地方开始。在这种情况下,下一组从 25 开始

请询问是否有任何疑问

4

4 回答 4

2
$nums = 105;
$rows = 8;
$colsize = 3;

$col = floor($nums / $rows);
$group = floor($col / $colsize);
$count = 0;
$groupsize = $rows * $colsize;
for ($g = 0; $g <= $group; $g++) {
    echo "<div class='group'>";

    $modulo = 0;
    $correction = 0;
    $rest = $nums - $count;
    if ($rest < $groupsize) {
        $empty = $groupsize - $rest;
        $correction = floor($empty / $colsize);
        $modulo = $empty % $colsize;
    }
    for ($i = 1; $i <= $rows; $i++) {
        $colind = 0;
        for ($j = $i; $j <= $groupsize; $j = $j + $rows) { 
            $count++;
            if ($count > $nums) {
                break;
            }
            $val = $j + ($g * $groupsize);

            $val -= $colind * $correction;
            $modcor = $colind - ($colsize - $modulo);
            if ( $modcor > 0 ) {
                $val -= $modcor;
            }
            echo "<div class='fleft'>" . $val . "</div>";
            $colind++;
        }
        echo "<div class='clear'></div>";
    }
    echo "</div>";
}

这有效:

在此处输入图像描述

此外,您可以更改位数、列数或列的大小

于 2013-11-08T10:25:21.960 回答
0

可能是我疯了,做了一个简单的改变....试试这个

$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$letCounter=0;  //added a counter

for ($g = 0; $g <= $group; $g++) {

    echo "<div class='group'>";
    for ($i = 1; $i <= $rows; $i++) {
        $letCounter=0; //reset counter on each loop
        for ($j = $i; $j <= 24; $j = $j + $rows) 
        {
         $count++;
         if($count>$nums)
         {break;}
        //made an alter  in the below line , some math :)
         echo "<div class='fleft'>".($letCounter++ * $rows +$i)."</div>";  
        }
        echo "<div class='clear'></div>";
    }
    echo "</div>";
}

谢谢 !

于 2013-11-08T08:35:50.907 回答
0
for($group = 0; $group < 3; $group++){
    for($row =1 ; $row <= 8; $row++){
        for($col = 0; $col <= 2; $col++){
            echo ($group*24)+ $row + 8 * $col; echo " ";
        }
        echo "\n";
    }
}

此代码将以请求的格式打印数字。您需要根据需要进行修改。

于 2013-11-08T08:30:12.440 回答
0

这个五月有效

$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$flag = true;
for($c=1;$c<=$col;$c++)
{

        if($c%$group== 1)
        {
            echo "Group Start";
            $flag = false;
        }
        for ($i = 1; $i <= $rows; $i++) {
                $count++;
                echo "<div class='fleft'>$count</div>";

            echo "<div class='clear'></div>";

        }
        echo "Line End";
        if($c%$group == 2&& $flag)// Check here for your requirement        
            echo "Group End </br>";
        $flag = true;

}
于 2013-11-08T09:27:53.490 回答