Задача: Food Lines (Очереди за продуктами)

    0

    0

    algorithmic-thinkingalgorithm
    Даниель Зингаро "Алгоритмы на практике"

    <?php
    //количество очередей
    $linesCount = 5;
    
    //количество новых людей
    $newPeople = 7;
    
    //количество человек в каждой из очередей до прихода новых людей
    $lines = [3, 2, 3, 2, 1];
    
    //решение
    function solve(&$lines, $linesCount, $newPeople) {
    
      $shortest = null;
    
      for ($i = 0; $i < $newPeople; $i++) {
        $shortest = shortestLineIndex($lines, $linesCount);
    
        $lines[$shortest]++;
      }
    
      return $lines;
    }
    
    //вспомогательная функция
    //поиск очереди с минимальным количеством людей
    function shortestLineIndex($lines, $linesCount) {
    
      $shortest = 0;
    
      for ($i = 1; $i < $linesCount; $i++) {
        if ($lines[$i] < $lines[$shortest]) {
          $shortest = $i;
        }
      }
    
      return $shortest;
    }
    
    print_r(solve($lines, $linesCount, $newPeople));
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.