2017-03-02 41 views
0

我有很多參與者和一些組,我必須組織參與者。如何組塊分塊不準確?

例子:

10/3 = 3, 3 and 4. 
10/9 = 2,2,2 and 4. 
23/3 = 6,6,6 and 5. 

我已經使用了大小paramether參與者/組的圓形結果與array_chunk嘗試,但它沒有很好地工作。

編輯與我的問題解決。

$groups   = $this->request->data['phases_limit']; 
    $classified_lmt = $this->request->data['classified_limit']; 

    $participants  = count($game->user_has_game); 

    $participants_lmt = floor($participants/$groups); 
    $remainders  = $participants % $groups; 

    if ($groups > $participants) { 
     throw new \Exception("Há mais grupos que participantes"); 
    } 

    for ($i=0; $i < $groups; $i++) { 
     $p = $this->Phase->newEntity(); 
     $p->name    = 'Grupo #' . $game->id; 
     $p->game_id   = $game->id; 
     $p->classified_limit = $classified_lmt; 
     $this->Phase->save($p); 

     // add the number of participants per group 
     for ($j=0; $j < $participants_lmt; $j++) { 
      $user_has_game = array_pop($game->user_has_game); 
      $g     = $this->Phase->GroupUserHasGame->newEntity(); 
      $g->group_id   = $p->id; 
      $g->user_has_game_id = $user_has_game->id; 
      $this->Phase->GroupUserHasGame->save($g); 
     } 

     // check if it is the last iteration 
     if (($groups - 1) == $i) { 
      // add the remainders on the last iteration 
      for ($k=0; $k < $remainders; $k++) { 
       $user_has_game = array_pop($game->user_has_game); 
       $g     = $this->Phase->GroupUserHasGame->newEntity(); 
       $g->group_id   = $p->id; 
       $g->user_has_game_id = $user_has_game->id; 
       $this->Phase->GroupUserHasGame->save($g); 
      } 
     } 
    } 
+1

如果你的問題與CakePHP有關,那麼請指出,只是把它標記爲相關不是很有意義 - 謝謝! – ndm

+0

@ndm我標記了CakePHP,因爲我的腳本正在運行,如果它有一些功能可以幫助我,那會更好。 –

回答

2

你試過模運算符嗎?它將分母除以分母后的餘數。

例如,如果你想10人分成3組:

floor(10/3) = 3; // people per group 
10 % 3 = 1; // 1 person left over to add to an existing group. 

編輯 - 我包括下面的功能我原來的答案的一部分。這對於OP來說不起作用,但是我想把它留在這裏,因爲它可以幫助其他人。

function group($total, $groups) 
{ 
    // Calculate participants per group and remainder 
    $group = floor($total/$groups); 
    $remainder = $total % $groups; 

    // Prepare groupings and append remaining participant to first group 
    $groupings = array_fill(0, $groups, $group); 
    $groupings[0] += $remainder; 

    return $groupings; 
} 
+0

是的,我曾嘗試模數運算符,但它沒有奏效。你關於模數的提示讓我再試一次,它的工作。 –

+0

如果您剪下功能部件,我希望將您的答案標記爲已接受。 –

+0

@RayannNayran - 我很好奇。功能有什麼問題? – fubar

0

不知道有沒有現成的庫。我只是在Java中實現了類似的東西,如果你需要一些想法:

public List<Integer> createDistribution(int population_size, int groups) { 
     List<Integer> lst = new LinkedList(); 
     int total = 0; 
     for (double d : createDistribution(groups)) { 
      // this makes smaller groups first int i = new Double(population_size * d).intValue(); 
      int i = (int)Math.round(population_size * d); 
      total += i; 
      lst.add(i); 
     } 
     // Fix rounding errors 
     while (total < population_size) { 
      int i = r.nextInt(groups); 
      lst.set(i, lst.get(i) + 1); 
      total += 1; 
     } 
     while (total > population_size) { 
      int i = r.nextInt(groups); 
      if (lst.get(i) > 0) { 
       lst.set(i, lst.get(i) - 1); 
       total -= 1; 
      } 
     } 
     return lst; 
    } 
+0

謝謝你的回答。羅布關於模數的小技巧幫助我解決了我的問題。 –