Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $bottle1 = 5;
- $bottle2 = 7;
- $bottle3 = 11;
- $bottles = [
- "bottle 1" => [
- "capacity" => $bottle1,
- "total" => 0
- ],
- "bottle 2" => [
- "capacity" => $bottle2,
- "total" => 0
- ],
- "bottle 3" => [
- "capacity" => $bottle3,
- "total" => 0
- ]
- ];
- $milk = 100;
- arsort($bottles); // sort by highest value
- $bottle_keys = array_keys($bottles);
- $bottles[$bottle_keys[0]]["total"] = floor($milk / $bottles[$bottle_keys[0]]["capacity"]);
- $last_milk = $milk - ($bottles[$bottle_keys[0]]["total"] * $bottles[$bottle_keys[0]]["capacity"]);
- $last_index = count($bottles) - 1;
- if ($last_milk > 0) {
- do {
- print_r($last_milk);
- if ($last_milk - $bottles[$bottle_keys[$last_index]]["capacity"] < 0) {
- break;
- }
- $last_index--;
- } while($last_index >= 0);
- $bottles[$bottle_keys[$last_index]]["total"] += 1;
- }
- $sum = 0;
- foreach ($bottles as $bottle_name => $bottle) {
- echo $bottle_name . " (capacity -> ". $bottle["capacity"] . ")= " . $bottle["total"] . "\n";
- $sum += $bottle["total"];
- }
- echo "total " . $sum . " bottles";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement