开发者

Static sized permutations from Dynamic sized set

开发者 https://www.devze.com 2023-04-02 03:57 出处:网络
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 thi

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.


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..

0

精彩评论

暂无评论...
验证码 换一张
取 消