如何以包含大约相同数量的男孩和女孩的方式将一些学生分成不同的组

How to divide a number of students into different groups in a way that contains approximately the same number of boys and girls

我正在做一个学校编程项目,我需要给每个学生分配一个小组,这样所有小组的男孩和女孩人数大致相同。

此外,老同学保持同一个组,所以我最初有 2 个组(A 和 B),其中一些学生已经分配,​​并且有一个分配组的列表。每个小组都有允许的最大学生人数。

例如:

-之前分配的学生人数:

          Group A                     Group B
       --------------             ---------------
        Boys      6                 Boys     12
        Girls     9                 Girls     8
       --------------             ---------------
        Total     15                Total     20

-要分配的学生人数:

       --------------             ---------------
        Boys      8                 Girls     4
       --------------             ---------------

-每组学生人数上限:

       ------------------------------------------
                  25 students per group
       ------------------------------------------

-求得解:

          Group A                     Group B
       --------------             ---------------
        Boys      13                 Boys     13
        Girls     11                 Girls    10
       --------------             ---------------
        Total     24                Total     23

-我得到这个解决方案的过程如下:

1. I have calculated the total number of boys and girls:

  Total Boys: 6 + 12 + 8 = 26
  Total Girls: 9  + 8 + 4 = 21

2. I have calculated half of both amounts:

  Total Boys:   26 / 2 = 13
  Total Girls:  21 / 2 = 10 (10.5)

3. I have calculated the difference between the students assigned and the 
   students remaining to be assigned until the amount obtained previously:

          Group A                     Group B
       --------------             ---------------
       Boys   13-6= 7               Boys   13-12= 1
       Girls  10-9= 1               Girls  10-8= 2
       --------------             ---------------


4. I have assigned the necessary amount of the students in each group:

          Group A                     Group B
       --------------             ---------------
       Boys   6+7= 13              Boys   12+1= 13
       Girls  9+1= 10              Girls  8+2=  10
       --------------             ---------------
        Total     23                Total     23

5. And finally the one that I have left I have added it to the first 
   group:

          Group A                     Group B
       --------------             ---------------
       Boys   13                   Boys   13
       Girls  10+1=11              Girls  10
       --------------             ---------------
        Total     24                Total     23

我需要知道如何使作业对之前指定的任意数量的学生都有效。我还需要对三个组(A、B 和 C)执行相同的过程。

提前致谢

你提出的解决方案很好。 请记住将学生人数除以 step #2 中的小组人数。这应该足以使您的解决方案灵活。