Python:0-9的十位数字策略性遍历

2024-04-27 10:51:20 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我读到一道数学题,启发我写一个程序。它要求将数字0-9排列一次,这样xx-xxx/xx-xxx=9我编写了一个python程序来找到解决方案,但在确保数字不同方面遇到了一些麻烦。我找到了一种使用嵌套whilesifs的方法,但我对此并不太满意。在

b,c,x,y,z = 0,0,0,0,0  #I shortened the code from b,c,d,e,v,w,x,y,z
for a in range (10):
    while b < 10:
        if b != a:
            while c < 10:
                if c != b and c != a:
                    while x < 10:
                        if x != c and x != b and x != a:
                            while y < 10:
                                if y != x and y != c and y != b and y != a:
                                    while z < 10:
                                        if z != y and if z != z and y != c and z != b and z != a:
                                            if (a*100 + b*10 + c)/(x*100 + y*10 + z) == 9:
                                                print ()
                                                print (str (a*100 + b*10 + c) + "/" + str (x*100 + y*10 + z)
                                        z += 1
                                    z = 0
                                y += 1
                            y,z = 0,0
                        x += 1
                    x,y,z = 0,0,0
                c += 1
            c,x,y,z = 0,0,0,0
        b += 1
    b,c,x,y,z = 0,0,0,0,0

正如您所见,代码非常长且重复性很强,甚至缩写形式也是如此。在我的笔记本电脑上运行它几乎需要一分钟时间(而且我的笔记本是新的)。我一直在寻找答案,但我只找到了生成随机数的方法。我试着用itertools.排列也是,但这只显示排列,而不是创建一个数字。在

生成全部10个数字的时间太长了,我想知道是否有一种使用python3的更快、更简单的方法,并给出解释。。在

谢谢


Tags: and方法程序if时间数字解决方案xxx
3条回答

这里有一种使用itertools来解决这个问题的方法。在

import itertools

def makenum(digits):
    return int(''.join(map(str, digits)))

for p in itertools.permutations(range(10)):
    a = makenum(p[:5])
    b = makenum(p[5:])
    if a == 9 * b:
        print(a, b)

根据Wayne Werner的解决方案,您可以这样做来添加数字唯一性约束(假设Python3):

[(9*num, num) 
 for num in range(10000, 100000 // 9) 
 if len(set(str(num) + str(num * 9))) == 10]

这在我的机器上运行1.5毫秒。在

请注意,您只能检查10000到100000之间的数字/9=11111。在

如果要允许前面的零,可以这样做:

^{pr2}$

这个需要15毫秒

利用代数:

a / b = 9 == a = 9 * b

知道了这一点,您只需费心生成值:

^{pr2}$

如果需要根据某些条件过滤掉,可以很容易地编写一个过滤函数:

def unique_numbers(num):
    num = str(num)
    return len(num) == len(set(num))

[(9*num, num) for num in range(10000, 100000) if unique_numbers(num) and unique_numbers(9*num)]

如果你想缩短一点,你可以重写你的函数,让它返回有效的对,否则None。在

def good_nums_or_none(num):
    a = num * 9
    b = num
    str_a = str(a)
    str_b = str(b)
    if len(a) == len(set(a)) and len(b) == len(set(b)):
         return a, b
    else:
         return None

[nums for nums in (good_nums_or_none(num) for num in range(10000, 100000)) if nums is not None]

或者,只需创建一个生成器并对其进行迭代:

 def target_numbers(factor=9, min=10000, max=100000):
     cur = min
     while cur < max:
         a = factor*cur
         b = cur
         str_a = str(a)
         str_b = str(b)
         if len(a) == len(set(a)) and len(b) == len(set(b)):
             yield a, b

 [num for num in target_numbers()]

如果希望在b中允许零填充数字,则可以使用此筛选器:

def target_numbers(factor=9, min=1000, max=100000):
    cur = min                                      
    while cur < max:                               
        b = cur                                    
        a = factor*cur                             
        text = str(a) + str(b).zfill(5)            
        if len(text) == len(set(text)):            
            yield a, b                             
        cur += 1    

相关问题 更多 >