暴力算法停止循环

时间:2016-11-11 11:20:38

标签: python

我正在尝试将三个数字的组合添加到810并乘以10760040.这是我到目前为止所拥有的;

x = 1
y = 1
z = 1

keepGoing = 1

while keepGoing == 1:
    if x+y+z == 810:
        if x*y*z == 10760040:
            print(x)
            print(y)
            print(z)
        else:
            keepGoing = 2
            z = 1
            print("no z")
        elif z == 810:
        keepGoing = 2
        print("no")
    else:
        z += 1




while keepGoing == 2:
    if x+y+z == 810:
        if x*y*z == 10760040:
            print(x)
            print(y)
            print(z)
        else:
            keepGoing = 3
            z = 1
            y = 1
            print("no y")

    elif y == 810:
        keepGoing = 3
    else:
        z += 1
        keepGoing = 1

while keepGoing == 3:
    if x+y+z == 810:
        if x*y*z == 10760040:
            print(x)
            print(y)
            print(z)
            print("Success")
        else:
            print("Failure")
    elif x == 810:
        print("none")
    else:
        x += 1
        keepGoing = 1

我知道这段代码基本上是基本的,但是我很久没有做过任何Python了,而且我想找到一个数字列表,添加到810并乘以10760040,任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:3)

更简洁的代码将是:

for x in range(1,810):
    for y in range(1,810-x):
        z = 810-x-y
        if x*y*z == 10760040:
            print 'x={},y={},z={}'.format(x,y,z)