问题
How can I obtain all combinations for a list where combination size need only be static eg. if the list has 4 elements then it will only need permutations of length 4 not 3, 2 and 1. I'm guessing this will need recursion. Unique combinations would be helpful but I'd like to see it in simplest (no uniqueness?) form for my puppy power.
回答1:
set s = { x1, x2, x3, x4 };
array solution;
permute( i ) =>
if( i == 0 ) => print and return;
while unused elements in set =>
take element from set which is not in solution;
put element in solution;
permute( i - 1 );
remove element from solution;
If you want a more specific answer you have to create a more specific question. Show some code/effort/whatever..
来源:https://stackoverflow.com/questions/7281303/static-sized-permutations-from-dynamic-sized-set