<?php
header("Content-Type: text/plain; charset=utf-8");
class MoneyDivide
{
/**
* @var int
*/
private $amount;
/**
* @param int $amount
*/
public function __construct($amount)
{
$this->amount = $amount;
}
/**
* @return array
*/
public function getStepsTask1()
{
return $this->calculateTask1($this->amount);
}
/**
* @return array
*/
public function getStepsTask2()
{
return $this->calculateTask2($this->amount);
}
/**
* @param int $amount
* @return array [min,max]
*/
private function calculateTask1($amount)
{
if ($amount < 4) {
return [1,1];
}
$dividedAmount = floor($amount / 2); $remaining = $amount % 2;
$stepsDivided = $this->calculateTask1($dividedAmount);
$stepsDivided[1] += 1;
$stepsDivided[0] = $stepsDivided[1];
if ($remaining > 0) {
$stepsDivided[0] = 1;
}
return $stepsDivided;
}
/**
* @param int $amount
* @return array [min,max]
*/
private function calculateTask2($amount)
{
if ($amount < 4) {
return [1,1];
}
$dividedAmount = floor($amount / 2); $remaining = $amount % 2;
$stepsDivided = $this->calculateTask2($dividedAmount);
$stepsDivided[1] += 1;
$stepsDivided[0] = $stepsDivided[1];
if ($remaining > 0) {
$stepsDivided[0] = 1;
}
if ($amount == $this->amount) {
$stepsDivided[1] += 1;
}
return $stepsDivided;
}
}
$amount = 12;
$calculator = new MoneyDivide($amount);
$stepsTask1 = $calculator->getStepsTask1();
echo 'Задача 1 для ' . $amount . ' монеток. Требуется взвешиваний: ';
if ($stepsTask1[0] == $stepsTask1[1]) {
echo $stepsTask1[1];
} else {
echo $stepsTask1[0] . '-' . $stepsTask1[1];
}
$stepsTask2 = $calculator->getStepsTask2();
echo "\nЗадача 2 для " . $amount . ' монеток. Требуется взвешиваний: ';
if ($stepsTask2[0] == $stepsTask2[1]) {
echo $stepsTask2[1];
} else {
echo $stepsTask2[0] . '-' . $stepsTask2[1];
}