货架堆叠的伪代码

发布于 2024-07-29 22:49:24 字数 195 浏览 5 评论 0原文

假设我有一些宽度为 1-n 个单位的序列编号项目,需要按行显示。 每行的宽度为 m 个单位。 我需要一些伪代码来为我输出行,以便保留 m 宽度限制。 这不是背包问题,因为项目必须保持序列号顺序 - 行末尾的空格没问题。

我一直在追寻这个问题,部分原因是我在 PHP 和 jQuery/javascript 中都需要它,因此需要伪代码......

Suppose I have some serially numbered items that are 1-n units wide, that need to be displayed in rows. Each row is m units wide. I need some pseudo-code that will output the rows, for me, so that the m-width limit is kept. This is not a knapsack problem, as the items must remain in serial number order - empty spaces at the end of rows are fine.

I've been chasing my tail over this, partly because I need it in both PHP and jQuery/javascript, hence the request for pseudo-code....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三生殊途 2024-08-05 22:49:24
while (!items.isEmpty()) {
  rowRemain = m;
  rowContents = [];
  while (!items.isEmpty() && rowRemain > items[0].width) {
    i = items.shift();
    rowRemain -= i.width
    rowContents.push(i);
  }
  rows.push(rowContents);
}

运行时间为 θ(项目数)

while (!items.isEmpty()) {
  rowRemain = m;
  rowContents = [];
  while (!items.isEmpty() && rowRemain > items[0].width) {
    i = items.shift();
    rowRemain -= i.width
    rowContents.push(i);
  }
  rows.push(rowContents);
}

Running time is Θ(number of items)

顾铮苏瑾 2024-08-05 22:49:24

模数是你的朋友。 我会做类似的事情:

$items = array(/* Your list of stuff */);
$count = 0;
$maxUnitsPerRow = 4; // Your "m" above

while ($item = $items[$count]) {
 if ($count % $maxUnitsPerRow == 0) {
    $row = new row();
 }
$row->addItemToRow($item);
$count++;
}

Modulus is your friend. I would do something like:

$items = array(/* Your list of stuff */);
$count = 0;
$maxUnitsPerRow = 4; // Your "m" above

while ($item = $items[$count]) {
 if ($count % $maxUnitsPerRow == 0) {
    $row = new row();
 }
$row->addItemToRow($item);
$count++;
}
乱了心跳 2024-08-05 22:49:24

这是一个替代的 php 代码...

function arrayMaxWidthString($items, $maxWidth) {
    $out = array();
    if (empty($items)) {
        return $out;
    }

    $row = $maxWidth;
    $i = 0;

    $item = array_shift($items);
    $row -= strlen($item);
    $out[0] = $item;

    foreach ($items as $item) {
        $l = strlen($item);
        $tmp = ($l + 1);
        if ($row >= $tmp) {
            $row -= $tmp;
            $out[$i] = (($row !== $maxWidth) ? $out[$i] . ' ' : '') . $item;
        } elseif ($row === $maxWidth) {
            $out[$i] = $item;
            ++$i;
        } else {
            ++$i;
            $row = $maxWidth - $l;
            $out[$i] = $item;
        }
    }
    return $out;
}

Here is an alternative php code ...

function arrayMaxWidthString($items, $maxWidth) {
    $out = array();
    if (empty($items)) {
        return $out;
    }

    $row = $maxWidth;
    $i = 0;

    $item = array_shift($items);
    $row -= strlen($item);
    $out[0] = $item;

    foreach ($items as $item) {
        $l = strlen($item);
        $tmp = ($l + 1);
        if ($row >= $tmp) {
            $row -= $tmp;
            $out[$i] = (($row !== $maxWidth) ? $out[$i] . ' ' : '') . $item;
        } elseif ($row === $maxWidth) {
            $out[$i] = $item;
            ++$i;
        } else {
            ++$i;
            $row = $maxWidth - $l;
            $out[$i] = $item;
        }
    }
    return $out;
}
混吃等死 2024-08-05 22:49:24

对于它的价值,我想我已经找到了我正在寻找的 PHP - 但不确定是否有更简单的方法......

<?php
// working with just a simple array of widths...
$items     = array(1,1,1,2,1,1,2,1);
$row_width = 0;
$max_width = 2;

echo "Begin\n"; // begin first row
foreach($items as $item=>$item_width) {
  // can we add item_width to row without going over?
  $row_width += $item_width;
  if($row_width < $max_width) {
    echo "$item_width ";
  } else if($row_width == $max_width) {
    echo "$item_width";
    echo "\nEnd\nBegin\n"; // end last row, begin new row
    $row_width = 0;
  } else if($row_width == 2* $max_width) {
    echo "\nEnd\nBegin\n"; // end last row, begin new row
    echo "$item_width";
    echo "\nEnd\n"; // end new row
    $row_width = 0;
    if($item < count($items)) echo "Begin\n"; // new row
  } else if($row_width > $max_width) {
    echo "\nEnd\nBegin\n"; // end last row, begin new row
    echo "$item_width";
    $row_width = $item_width;
  }
}
echo "\nEnd\n"; // end last row

?>

For what it's worth, I think I have what I was looking for, for PHP - but not sure if there is a simpler way...

<?php
// working with just a simple array of widths...
$items     = array(1,1,1,2,1,1,2,1);
$row_width = 0;
$max_width = 2;

echo "Begin\n"; // begin first row
foreach($items as $item=>$item_width) {
  // can we add item_width to row without going over?
  $row_width += $item_width;
  if($row_width < $max_width) {
    echo "$item_width ";
  } else if($row_width == $max_width) {
    echo "$item_width";
    echo "\nEnd\nBegin\n"; // end last row, begin new row
    $row_width = 0;
  } else if($row_width == 2* $max_width) {
    echo "\nEnd\nBegin\n"; // end last row, begin new row
    echo "$item_width";
    echo "\nEnd\n"; // end new row
    $row_width = 0;
    if($item < count($items)) echo "Begin\n"; // new row
  } else if($row_width > $max_width) {
    echo "\nEnd\nBegin\n"; // end last row, begin new row
    echo "$item_width";
    $row_width = $item_width;
  }
}
echo "\nEnd\n"; // end last row

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文