How can I find all of the permutations consisting of 1 element from a variable number of arrays of variable length?

后端 未结 4 1373
名媛妹妹
名媛妹妹 2020-12-12 02:36

I have an array U of arrays D that vary in length. I need to be able to return all permutations of array indices that would select a different perm

4条回答
  •  旧巷少年郎
    2020-12-12 03:30

    You could just keep a counter for your individual position in each array. In your get_next method increase the counter for one and mod it by the length of the array. Then you just increase the next counter every time the previous one rolls over to 0;

    if (pos3 == array_of_size_n3 -1)
    {
       if (pos2 == size_of_array_2 -1)
       {
           pos1 = (pos1 + 1) % size_of_array_1
    
       }
       pos2 = (pos2 + 1) % size_of_array_2
    }
    pos3 = (pos3 + 1) % size_of_array_3
    
    print array1[pos1], array2[pos2], array3[pos3]
    

    EDIT: In the case the number of arrays varies, hold your position variables in an array. Actually that would probably be better anyway. That way you can refer to the pos variable in the same way you refer to the array itself.

提交回复
热议问题