intelligently generating combinations of combinations

后端 未结 4 1347
遇见更好的自我
遇见更好的自我 2021-01-19 15:23

Let\'s say I have a class of 30 students and want generate every possible way in which they can be partitioned into groups of 5 (order is irrelevant).

I know how to

4条回答
  •  旧时难觅i
    2021-01-19 16:04

    You could do some post-processing on the permutations. Some pseudo-code (implement in the language of your choice...):

    // We have a list of lists called 'permutations'
    // combinations is an (empty) list of lists
    for each permutation in permutations
    {
       sortedPermutation = permutation.sort()
       if (! combinations.find(sortedPermutation) )
       {
           combinations.add(sortedPermutation);
       }
    }
    

    Probably not the most efficient; I'd add the sort & compare to the code that generates the permutations personally.

提交回复
热议问题