Професионална програма
Loading...
darimi27 avatar darimi27 8 Точки

Problem 13.*Speed Racing

60/100 точки в Judge.

https://pastebin.com/LBit19Kj

Втори и четвърти тест гърмят.Не намирам проблема.

Щеше да е хубаво , след около 50 опита да мине нещо в Judge , да се отключват скритите тестове smiley, за да можем да си намираме грешките сами.

Тагове:
1
PHP Web 04/09/2019 16:17:07
willystyle avatar willystyle 2314 Точки
<?php

class Car
{
    private $model;
    private $fuelAmount;
    private $fuelCostForKilometer;
    private $distance;
    /**
     * Car constructor.
     * @param $model
     * @param $fuelAmount
     * @param $fuelCostForKilometer
     * @param $distance
     */
    public function __construct(string $model, float $fuelAmount, float $fuelCostForKilometer, $distance = 0)
    {
        $this->setModel($model);
        $this->setFuelAmount($fuelAmount);
        $this->setFuelCostForKilometer($fuelCostForKilometer);
        $this->setDistance();
    }
    /**
     * @return string
     */
    public function getModel(): string
    {
        return $this->model;
    }
    /**
     * @param string $model
     */
    public function setModel(string $model): void
    {
        $this->model = $model;
    }
    /**
     * @return float
     */
    public function getFuelAmount(): float
    {
        return $this->fuelAmount;
    }
    /**
     * @param float $fuelAmount
     */
    public function setFuelAmount(float $fuelAmount): void
    {
        $this->fuelAmount = $fuelAmount;
    }
    /**
     * @return float
     */
    public function getFuelCostForKilometer(): float
    {
        return $this->fuelCostForKilometer;
    }
    /**
     * @param float $fuelCostForKilometer
     */
    public function setFuelCostForKilometer(float $fuelCostForKilometer): void
    {
        $this->fuelCostForKilometer = $fuelCostForKilometer;
    }
    /**
     * @return float
     */
    public function getDistance(): float
    {
        return $this->distance;
    }
    /**
     * @param float $distance
     */
    public function setDistance($distance = 0): void
    {
        $this->distance = $distance;
    }
    public function driveCar(float $driveDistance): bool
    {
        if ($this->getFuelCostForKilometer() * $driveDistance <= $this->getFuelAmount()) {
            // $this->setFuelAmount($this->getFuelAmount() - $this->getFuelCostForKilometer() * $driveDistance);
            $this->setFuelAmount(round($this->getFuelAmount() - $this->getFuelCostForKilometer() * $driveDistance), 2);
            $this->setDistance($this->getDistance() + $driveDistance);
            return true;
        }
        return false;
    }
    public function __toString(): string
    {
        $fuel = number_format($this->getFuelAmount(), 2, ".", "");
        return "{$this->getModel()} {$fuel} {$this->getDistance()}" . PHP_EOL;
    }
}

$n = intval(readline());
// require "Car.php";
$cars = [];
for($i = 0; $i < $n; $i++){
    list($model, $fuelAmount, $fuelCostForKilometer) = explode(" ", readline());
    $currentCar = new Car($model, $fuelAmount, $fuelCostForKilometer);
    $cars[$model] = $currentCar;
}
while(($input = readline()) != "End"){
    list($instruction, $carModel, $driveDistance) = explode(" ", $input);
    $result = $cars[$carModel]->driveCar($driveDistance);
    if(!$result){
        echo "Insufficient fuel for the drive" . PHP_EOL;
    }
}
foreach ($cars as $car){
    echo $car;
}

 

0
willystyle avatar willystyle 2314 Точки

Горното е едно чуждо решение, което леко преправих да мине на 100%, оказа се, че в твоето 2 проблемни теста са закръгляне при всяка операция с горивото и number_format последващите параметри(десетична точка). Ето и рефактор на твоето решение, пооправил съм класа да има само необходимата функционалност:

<?php

class Car
{
    private $model;
    private $fuelAmount; //налично гориво
    private $fuelCostOneKilometer;
    private $distanceTraveled; //пропътувани км

    public function __construct(
        string $model,
        float $fuelAmount,
        float $fuelCostOneKilometer
    ) {
        $this->model = $model;
        $this->fuelAmount = $fuelAmount;
        $this->fuelCostOneKilometer = $fuelCostOneKilometer;
        $this->distanceTraveled = 0;
    }

    public function getModel(): string
    {
        return $this->model;
    }

    public function getFuelAmount(): float
    {
        return $this->fuelAmount;
    }

    public function getDistanceTraveled(): float
    {
        return $this->distanceTraveled;
    }

    public function Drive($amountOfKm) :void
    {
        if ($this->fuelAmount >= $amountOfKm * $this->fuelCostOneKilometer) {
            $this->fuelAmount = round($this->fuelAmount - $amountOfKm * $this->fuelCostOneKilometer, 2);
            $this->distanceTraveled = $this->distanceTraveled + $amountOfKm;
        } else {
            echo "Insufficient fuel for the drive" . PHP_EOL;
        }
    }
}

$n = intval(readline());
$cars = [];
for ($i = 0; $i < $n; $i++) {
    $input = explode(" ", readline());
    $model = $input[0];
    $fuelAmount = $input[1];
    $fuelCostOneKilometer = $input[2];
    $car = new Car($model, $fuelAmount, $fuelCostOneKilometer);
    array_push($cars, $car);    
}
$input = readline();

while ($input != "End") {
    $input = explode(" ", $input);
    $carModel = $input[1];
    $amountOfKm = $input[2];
    for ($i = 0; $i < count($cars); $i++) {
        $currentCar = $cars[$i];
        if ($carModel === $currentCar->getModel()) {
            $currentCar->Drive($amountOfKm);
            break;
        }
    }
    $input = readline();
}
foreach ($cars as $car) {
    echo $car->getModel() . " " . number_format($car->getFuelAmount(), 2, ".", "") . " " . $car->getDistanceTraveled() . PHP_EOL;
}

 

1