The demand for an algorithm is as follows: There are several groups of original users (group sequence A), the number of users in each group is not fixed, and the number of users in each group is not repeated, each user only appears in one group. Now we want to rearrange the new group sequence B. New Group requirements: 1. The number of group sequence B is based on the group...
The requirement for algorithm is as follows:
There are several groups of original users (group sequence A). The number of users in each group is not fixed. The number of users in each group is unique, and each user is only in one group.
To rearrange the new group sequence B, the new group requirements are as follows:
1. the number of groups in group sequence B is determined by the maximum number of group members in group sequence A. For example, the maximum number of members in Group A-group 1 is N, then the number of group sequence B is N.
This is also to prevent group A members from appearing in the same group B at the same time.
2. As explained above, members in each group of A cannot appear in Group B when two people exist in Group B at the same time, in order to make the results fair.
3. Change the members of Group A according to certain algorithm rules, so that the number of members in Group B is less than or equal to 1.
The program is php. It would be even better if you could provide this function directly. Thank you!
For example: function adjust_array ($ arrayA ){
Return $ arrayB;
}
Reply content:
The requirement for algorithm is as follows:
There are several groups of original users (group sequence A). The number of users in each group is not fixed. The number of users in each group is unique, and each user is only in one group.
To rearrange the new group sequence B, the new group requirements are as follows:
1. the number of groups in group sequence B is determined by the maximum number of group members in group sequence A. For example, the maximum number of members in Group A-group 1 is N, then the number of group sequence B is N.
This is also to prevent group A members from appearing in the same group B at the same time.
2. As explained above, members in each group of A cannot appear in Group B when two people exist in Group B at the same time, in order to make the results fair.
3. Change the members of Group A according to certain algorithm rules, so that the number of members in Group B is less than or equal to 1.
The program is php. It would be even better if you could provide this function directly. Thank you!
For example: function adjust_array ($ arrayA ){
Return $ arrayB;
}
$arrayB = [];$max = array_reduce($arrayA,function($varA,$varB){ return ($varA>count($varB))? $varA:count($varB)},0);$counter = 0;$row = 0;foreach($arrayA as $arrayBefore) { foreach($arrayBefore as $item) { if ($counter>=$max) { $counter = 0; $row++; } $arrayB[$row][$counter++] = $item; }}return $arrayB;
It seems not that complicated. You can calculate the number of all users and the number of groups first, and then sort them out. For example, if there are 34 people in five groups, each group should have at least 6 people and a maximum of 7 people. Define an array for free people. If there are more than 7 people in a group, put them in free, if there are less than six, we can retrieve them from the free people. If there are more than one free people, We can traverse them again and put them in the group of six people.