![](/img/trans.png)
[英]Print all n-digit numbers whose product of digits equals to given product python
[英]How to write program in python to generate all n digit numbers with only 4 and 7 as their digits?
我嘗試使用itertools.permutations生成排列,但是我對如何使用n位數字感到困惑。
我會改用itertools.product
:
In [26]: for i in itertools.product(['4', '7'], repeat=2):
....: print int(''.join(i))
....:
44
47
74
77
repeat
參數是您的n
。
我會用二進制的,如果你需要的所有2位數的數字只有7
, 4
作為數字:
以2為基的最大2位數字是11b
即3
,所以:
0 => 00b
1 => 01b
2 => 10b
3 => 11b
然后將0
替換為4
,將1
替換為7
(任意),得到: 44, 47, 74, 77
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.