[Löst] Santa's Cookies - PHP Task

  

3
Ämnesstart

You need to make cookies for Santa and his dwarfs.

You will receive the amount of batches – n that you need to bake. For every batch you will receive ingredients:  flour, sugar and cocoa in grams, each on a new line. You need to calculate how many boxes of cookies you get for every batch with the given ingredients and total boxes of cookies for all batches. To calculate the number of boxes per batch you need to divide total cookies per bake by cookies per box (see the table below). To get the total cookies per bake use the following formula and round the result to the nearest lower number:  

({cup} + {smallSpoon} + {bigSpoon}) * min from({flourCups}, {sugarSpoons}, {cocoaSpoons}) / singleCookieGrams

To get the flourCups divide flour by cups.

To get the sugarSpoons divide sugar by bigSpoon.

And for the cocoaSpoons divide cocoa by smallSpoon.

The cups and the spoons must be integer numbers.

(see the table below)

If flourCups, sugarSpoons or cocoaSpoons for a single bake are not enough (<=0), print the following message: "Ingredients are not enough for a box of cookies."

Otherwise calculate the cookies and print the number of boxes you get for the current batch:
"Boxes of cookies: {boxes of cookies per current bake}"

Use the following table for calculations:

santas cookies php task

When you finish baking, pack the all the cookies in boxes and send them to Santa and his dwarfs and print the total number of boxes on the console.

"Total boxes: {totalBoxes for all bakes}"

Input:
The input data should be read from the console. It will consist of:

  • Amount of batches - integer  number in range [0…1,000,000,000]

For every batch:

  • Amount of flour in grams – integer number in range [0…1,000]
  • Amount of sugar in grams – integer number in range [0…1,000]
  • Amount of cоcоа in grams – integer number in range [0…1,000]

The input data will always be valid and in the format described. There is no need to check it explicitly.

Output:
The output should be printed on the console.

  • If the ingredients for current bake are not enough:

"Ingredients are not enough for a box of cookies."

  • If the ingredients for current bake are enough:

"Boxes of cookies: {boxes of cookies per current bake}."

  • On the last line print:

"Total boxes: {totalBoxes for all bakes}"

Examples:

santas cookies php task examples

1 Answer
2

My solution:

<?php
 
$batches = intval(readline());
$totalBoxes = 0;
for ($i = 0; $i < $batches; $i++) {
    $currentFlour = floatval(readline());
    $currentSugar = floatval(readline());
    $currentCocoa = floatval(readline());
 
    $flourCups = floor($currentFlour / 140);
    $sugarSpoons = floor($currentSugar / 20);
    $cocoaSpoons = floor($currentCocoa / 10);
 
    $min = min($flourCups, $sugarSpoons, $cocoaSpoons);
    if ($min <= 0) {
        echo "Ingredients are not enough for a box of cookies." . PHP_EOL;
        continue;
    }
    $totalCookiesPerBake = (140 + 10 + 20) * $min / 25;
    $currentBoxes = floor($totalCookiesPerBake / 5);
    $totalBoxes += $currentBoxes;
 
    echo "Boxes of cookies: $currentBoxes" . PHP_EOL;
}
 
echo "Total boxes: $totalBoxes" . PHP_EOL;
Dela: