现有原材料:1000 克鱼片 成品菜:酸菜鱼大份需要 200 克鱼片和 酸菜鱼小份需要 100 克鱼片 用 php 如何实现平均分配原材料到大份酸菜鱼 3 份和小份酸菜鱼 4 份
这啥玩意?
// 原材料重量 $material = 1000;
// 一份酸菜鱼所需鱼片量 $bigFish = 200; $smallFish = 100;
// 酸菜鱼数量 $bigNum = 3; $smallNum = 4;
// 平均分配原材料到大份酸菜鱼和小份酸菜鱼 $bigFishWeight = $bigFish $bigNum; $smallFishWeight = $smallFish $smallNum; $totalFishWeight = $bigFishWeight + $smallFishWeight; // 总鱼片量
// 计算每份菜所需的原材料量 $averageWeight = $material / $totalFishWeight;
// 计算大份酸菜鱼和小份酸菜鱼所需要的鱼片量 $bigFishWeight = $averageWeight; $smallFishWeight = $averageWeight;
echo "大份酸菜鱼每份所需的鱼片量为:{$bigFishWeight}克;\n"; echo "小份酸菜鱼每份所需的鱼片量为:{$smallFishWeight}克;\n";
for ($i=0; $i<=5; $i++) { for ($k=0; $k<=10; $k++) { if (($i*200+$k*100) === 1000) { echo '大份:'.$i.';小份:'.$k."\n"; } } }
以下条件随便设置
$total = 1100; // 或者其他 $a = 200; // 或者其他 $b = 100; // 或者其他 $acount = $bcount = floor($total / ($a + $b)); $rest = $total % ($a + $b); // 这里我优先把剩余材料(200g)做大份 if ($rest > $a) { $acount += 1; } else { $bcount += floor($rest / $b); } // 4大份 3小份 // 如果优先剩余材料做小份则直接 $bcount += floor($rest / $b); // 3大份 5小份
这啥玩意?
// 原材料重量
$material = 1000;
// 一份酸菜鱼所需鱼片量
$bigFish = 200;
$smallFish = 100;
// 酸菜鱼数量
$bigNum = 3;
$smallNum = 4;
// 平均分配原材料到大份酸菜鱼和小份酸菜鱼
$bigFishWeight = $bigFish $bigNum;
$smallFishWeight = $smallFish $smallNum;
$totalFishWeight = $bigFishWeight + $smallFishWeight; // 总鱼片量
// 计算每份菜所需的原材料量
$averageWeight = $material / $totalFishWeight;
// 计算大份酸菜鱼和小份酸菜鱼所需要的鱼片量
$bigFishWeight = $averageWeight;
$smallFishWeight = $averageWeight;
echo "大份酸菜鱼每份所需的鱼片量为:{$bigFishWeight}克;\n";
echo "小份酸菜鱼每份所需的鱼片量为:{$smallFishWeight}克;\n";
两层for循环,外层(大份)0~5,内层0~10
以下条件随便设置