用数字打印所有可能的组合

Print all of the possible combinations with numbers

本文关键字:组合 有可能 数字 打印      更新时间:2023-10-16

所以,我的想法很简单:打印给定X个数字的所有可能的组合。。。

例如,我有两个数字,1和0,所以程序打印:

(0,0)
(0,1)
(1,0)
(1,1)

有没有用C、C++或Python实现这一点的想法?(如果你知道如何用其他语言做到这一点,请无论如何帮助我)。

谢谢。

使用itertools.product。使用以下示例并根据需要扩展

>>> [x for x in itertools.product("01",repeat=2)]
[('0', '0'), ('0', '1'), ('1', '0'), ('1', '1')]
>>>