Софтуерно Инженерство
Loading...
+ Нов въпрос
mmilev_dev avatar mmilev_dev 0 Точки

Малък проблем със задача Feed the Animals от демо изпита

Здравейте колеги, от PHP групата съм и решавам въпросната задача и judge отпуска само 40 точки. Така и не успях да си намеря грешката та бих се радвал на малко помощ!

 

<?php

$animals = [];
$area = [];

$input = readline();

while ($input !== 'Last Info') {


    $args = explode(":", $input);
    $action = $args[0];
    $animalName = $args[1];
    $areaName = $args[3];

    if ($action == 'Add') {
        $dailyFoodLimit = intval($args[2]);

        if (!key_exists($animalName, $animals)) {
            $animals[$animalName] = intval($dailyFoodLimit);
            if (!key_exists($animalName, $area)) {
                $area[$areaName] = 1;
            } else
                $area[$areaName] += 1;

        } else {
            $animals[$animalName] += intval($dailyFoodLimit);
        }


    } else if ($action == 'Feed') {
        $givenFood = intval($args[2]);

        if (key_exists($animalName, $animals)) {
            $animals[$animalName] -= intval($givenFood);
            if ($animals[$animalName] <= 0) {
                echo "$animalName was successfully fed" . PHP_EOL;
                //unset($animals[$animalName]);
                $area[$areaName] -= 1;

            }
        }
    }

    $input = readline();
}


//sort animals
krsort($animals);
arsort($animals);


//output
echo 'Animals:' . PHP_EOL;
foreach ($animals as $hungryAnimal => $foodToGive) {
    if ($foodToGive > 0) {
        echo $hungryAnimal . " -> " . $foodToGive . 'g' . PHP_EOL;
    }
}

//sort areas
arsort($area);
echo 'Areas with hungry animals:' . PHP_EOL;

foreach ($area as $anArea => $count) {
    if ($count > 0) {
        echo $anArea . " : " . $count . PHP_EOL;
    }
}

 

Тагове:
0
Fundamentals Module
willystyle avatar willystyle 1795 Точки

две малки грешки:

1) if (!key_exists($animalName, $area))  --> тук е areaName не animalName

2) krsort($animals); --> тук е ksort($animals); сортиране по ключ възходящо

<?php
$animals = [];
$area = [];
$input = readline();
while ($input !== 'Last Info') {
    $args = explode(":", $input);
    $action = $args[0];
    $animalName = $args[1];
    $givenFood = intval($args[2]);
    $areaName = $args[3];
    if ($action == 'Add') {
        if (!key_exists($animalName, $animals)) {
            $animals[$animalName] = 0;
            if (!key_exists($areaName, $area)) {
                $area[$areaName] = 0;
            }
            $area[$areaName] += 1;
        }
        $animals[$animalName] += $givenFood;
    } else if ($action == 'Feed') {
        if (key_exists($animalName, $animals)) {
            $animals[$animalName] -= intval($givenFood);
            if ($animals[$animalName] <= 0) {
                echo "$animalName was successfully fed" . PHP_EOL;
                unset($animals[$animalName]);
                $area[$areaName] -= 1;
            }
        }
    }
    $input = readline();
}
//sort animals
ksort($animals);
arsort($animals);
//output
echo 'Animals:' . PHP_EOL;
foreach ($animals as $hungryAnimal => $foodToGive) {
    if ($foodToGive > 0) {
        echo $hungryAnimal . " -> " . $foodToGive . 'g' . PHP_EOL;
    }
}
//sort areas
arsort($area);
echo 'Areas with hungry animals:' . PHP_EOL;
foreach ($area as $anArea => $count) {
    if ($count > 0) {
        echo $anArea . " : " . $count . PHP_EOL;
    }
}

 

0