[英]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.