用于打印数组所有子集的所有排列的函数 [英] Function to print all permutations of all subsets of the array

查看:255
本文介绍了用于打印数组所有子集的所有排列的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打印出所有可能的子集,以及每个子集的所有可能的重新布置.

print out all the possible subsets of set and every possible rearrangement for each subset.

例如: s = [1,2]
s的子集= [[],[1],[2],[1,2]],
每个子集的每个可能排列: [[],[1],[2],[1,2],[2,1]],

Ex: s = [ 1 , 2 ]
subsets of s = [ [ ], [ 1 ], [ 2 ], [ 1, 2 ] ],
every possible permutation of each subset: [ [ ], [ 1 ], [ 2 ], [ 1, 2 ], [ 2, 1 ] ],

推荐答案

以下是使您了解如何执行操作的方法(您必须自己编写代码,特别是因为我不写C ++).最好的方法是递归:

Here is something to get you an idea how to do it(you have to write your code on your own, especially since I dont write C++). The best way to do this is recursion:

getSubcombinations(Combination comb, Set set){
     for a in set{
         newSet=set \ {a}
         getSubcombinations(comb+a, newSet)
     }
     print(comb);
}

该方法调用自身,使用元素"a"和剩余集的每个组合(不带a的集合)增加当前子集,如果不添加其他元素,则还给出当前子集.如果您的集合没有重复项(它是一个集合!),则不会产生重复的组合(这就是您要的,而不是子集!).

The method calls itself, increasing the current subset with an element "a" and every combination of the restset (set without a) and also gives out the current subset if you dont add further elements. If your set has no duplicates (it is a set!), this wont yield duplicate combinations (that is what you are asking for, not subsets!).

这篇关于用于打印数组所有子集的所有排列的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆