我需要弄清楚覆盖一个区域需要多少油漆,我遇到了问题。帮助我或给我一个想法
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
$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(会从大到小逐渐得到)
整个文件看起来像这样
<?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++;
}
看看变量名,有意义的变量名非常重要,如果你想让你的代码容易被支持。当然,您可以更改变量的值。
$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(会从大到小逐渐得到)
整个文件看起来像这样
<?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++;
}
看看变量名,有意义的变量名非常重要,如果你想让你的代码容易被支持。当然,您可以更改变量的值。