It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                已关闭8年。
            
                    
我想生成一组数字的大小n = 0, 1, 2, ...的子集。
相同的数字不应以不同的顺序重复,例如2-3-4 = 3-2-4 = 4-2-3 = 4-3-2

例如

vector<unsigned int> list = {2,4,6,8,9}


所以子集就像

n=0 {}

n=1 {2}{4}{6}{8}{9}

n=2 {2,4}{2,6}{2,8}{2,9}{4,6}{4,8}{4,9}{6,8}{6,9}{8,9}

最佳答案

生成所有长度等于您的数字位数的二进制数字。

3 numbers:
000
001
010
011
100
101
110
111


接下来,根据位置选择数字并将其映射到相应的集合(即如果将001映射为1,则将101映射为3)。

对于初始集{1,2,3}:

{}      ->0
{3}     ->1
{2}     ->1
{2,3}   ->2
{1}     ->1
{1,3}   ->2
{1,2}   ->2
{1,2,3} ->3


我只是给你一个主意,因为这看起来像是作业,而不是作业解决网站。这应该给您一个起点。

关于c++ - 如何在C++中生成给定数组的子集? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7769753/

10-13 08:19