输出一个由4个不重复的字母元素组成的集合的所有子集。例如:输入a b c d,则输出集合{a,b,c,d}的所有子集
时间: 2024-03-06 13:47:46 浏览: 56
求集合的所有子集
好的,我可以帮你解决这个问题。以下是实现该功能的 Python 代码:
```python
def generate_subsets(elements):
subsets = [[]]
for element in elements:
subsets += [subset + [element] for subset in subsets]
return subsets
elements = ['a', 'b', 'c', 'd']
subsets = generate_subsets(elements)
for subset in subsets:
print(subset)
```
输出结果为:
```
[]
['a']
['b']
['a', 'b']
['c']
['a', 'c']
['b', 'c']
['a', 'b', 'c']
['d']
['a', 'd']
['b', 'd']
['a', 'b', 'd']
['c', 'd']
['a', 'c', 'd']
['b', 'c', 'd']
['a', 'b', 'c', 'd']
```
这个算法的时间复杂度为 O(2^n),其中 n 是元素的数量。
阅读全文