【问题标题】:I need to figure out how much paint is needed to cover an area, I'm having problems with it. Help me or give me an idea我需要弄清楚覆盖一个区域需要多少油漆,我遇到了问题。帮助我或给我一个想法
【发布时间】:2020-10-27 02:31:18
【问题描述】:
$width = 10; //width
$length = 10; //Length
$door = 2; // Door area
$square = $width * $length - $door; //Total area to be painted
$perMeter = 1;  //Where $w is the amount of paint to paint 1m2
$total = $square * $perMeter; //The total amount of paint is sufficient for painting.
$containers = array(5,12,18,4,8,1); //The values for the number of containers are in liters.

需要多少桶 5,12,18,4,8,1(会逐渐从最大的部分到最小的部分)

【问题讨论】:

  • 至少你得知道公式。
  • 以最大容器对目标体积取模,然后对体积余数和更小的容器进行取模,直到只能减去 1s。 (甚至不需要背包算法。)

标签: php calculator


【解决方案1】:

整个文件可能是这样的

<?php

declare(strict_types=1);

$width = 10; //width
$length = 10; //Length
$door = 2; // Door area
$square = $width * $length - $door; //Total area to be painted
$perMeter = 1;  //Where $w is the amount of paint to paint 1m2
$total = $square * $perMeter; //The total amount of paint is sufficient for painting.

echo 'Total barrels needed: ' . $total . "barrels\n";

$containers = array(5,12,18,4,8,1); //The values for the number of containers are in liters.

rsort($containers);

$counter = 0;

while ($counter < count($containers)) {
    $total -= ($amount = intdiv($total, $containers[$counter])) * $containers[$counter];
    echo sprintf("%d barrel container - %d\n", $containers[$counter], $amount);
    $counter++;
}

查看变量名,有意义的变量名非常重要,如果您希望您的代码易于支持。当然你也可以改变变量的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    相关资源
    最近更新 更多