Generating Permutations using LINQ

后端 未结 8 1989
我寻月下人不归
我寻月下人不归 2020-12-01 04:59

I have a set of products that must be scheduled. There are P products each indexed from 1 to P. Each product can be scheduled into a time period 0 to T. I need to construct

8条回答
  •  被撕碎了的回忆
    2020-12-01 05:25

    1. create another array of length 2^n where n is the number of products
    2. count in binary from 0 to 2^n and fill in the array with each count. for example if n=3 the array will look like this :

    000 001 010 011 100 101 110 111

    1. loop through the binary array and find the ones in each number then add the product with the same index:
     for each binaryNumber in ar{
       for i = 0 to n-1{
         if binaryNumber(i) = 1
           permunation.add(products(i))
       }
      permunations.add(permutation) 
    }
    

    example: if binaryNumber= 001 then permunation1 = product1 if binaryNumber= 101 then permunation1 = product3,product1

提交回复
热议问题