Advertisement
dederusliandi

TEST CASE 1 JATIS

May 26th, 2022
570
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $bottle1 = 5;
  4. $bottle2 = 7;
  5. $bottle3 = 11;
  6. $bottles = [
  7.     "bottle 1" => [
  8.         "capacity" => $bottle1,
  9.         "total" => 0
  10.     ],
  11.     "bottle 2" => [
  12.         "capacity" => $bottle2,
  13.         "total" => 0
  14.     ],
  15.     "bottle 3" => [
  16.         "capacity" => $bottle3,
  17.         "total" => 0
  18.     ]
  19. ];
  20. $milk = 100;
  21.  
  22. arsort($bottles); // sort by highest value
  23.  
  24. $bottle_keys = array_keys($bottles);
  25. $bottles[$bottle_keys[0]]["total"] = floor($milk / $bottles[$bottle_keys[0]]["capacity"]);
  26. $last_milk = $milk - ($bottles[$bottle_keys[0]]["total"] * $bottles[$bottle_keys[0]]["capacity"]);
  27.  
  28. $last_index = count($bottles) - 1;
  29. if ($last_milk > 0) {
  30.     do {
  31.         print_r($last_milk);
  32.         if ($last_milk - $bottles[$bottle_keys[$last_index]]["capacity"] < 0) {
  33.             break;
  34.         }
  35.         $last_index--;
  36.     } while($last_index >= 0);
  37.    
  38.     $bottles[$bottle_keys[$last_index]]["total"] += 1;
  39. }
  40.  
  41. $sum = 0;
  42. foreach ($bottles as $bottle_name => $bottle) {
  43.     echo $bottle_name . " (capacity -> ". $bottle["capacity"] . ")= " . $bottle["total"] . "\n";
  44.     $sum += $bottle["total"];
  45. }
  46. echo "total " . $sum . " bottles";
  47. ?>
  48.  
Advertisement
RAW Paste Data Copied
Advertisement