Static sized permutations from Dynamic sized set

霸气de小男生 提交于 2019-12-25 00:22:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!