Python 如何检查每个可能的组合的有效性?
就上下文而言,我正试图找到这个问题的所有可行解决方案: 这是我到目前为止的代码,但是我对应该遍历所有可能组合的部分有问题Python 如何检查每个可能的组合的有效性?,python,loops,Python,Loops,就上下文而言,我正试图找到这个问题的所有可行解决方案: 这是我到目前为止的代码,但是我对应该遍历所有可能组合的部分有问题 x = 1 y = 1 z = 10 a = 10 while x < 10 and y < 10 and z < 100 and a < 100: #iterates through every possible combination x = x + 1 y = y + 1 z = z + 1 a = a + 1
x = 1
y = 1
z = 10
a = 10
while x < 10 and y < 10 and z < 100 and a < 100: #iterates through every possible combination
x = x + 1
y = y + 1
z = z + 1
a = a + 1
if x != y: #checks if x and are the same
if a/x == z/y or z/x == a/y: #checks if x and y are proportional to a and z
a = str(a) #converting each int to string
z = str(z)
x = str(x)
y = str(y)
if a.count(x) < 1 and a.count(y) < 1 and z.count(y) <1 and z.count(x) < 1: #checks if any number reapeats
print(x, y, z, a) #prints viable solution```
x=1
y=1
z=10
a=10
当x<10、y<10、z<100和a<100时:#迭代所有可能的组合
x=x+1
y=y+1
z=z+1
a=a+1
如果x!=y:#检查x和是否相同
如果a/x==z/y或z/x==a/y:#检查x和y是否与a和z成比例
a=str(a)#将每个整数转换为字符串
z=str(z)
x=str(x)
y=str(y)
如果a.count(x)<1,a.count(y)<1,z.count(y)您需要填充六个框。只需运行6个成员的所有排列并检查条件:
import itertools
for a,b,c,d,e,f in itertools.permutations([0,1,2,3,4,5,6,7,8,9],6):
if a == 0 or b == 0 or c == 0 or e == 0:
continue
if (10*c + d) / a == (10*e + f) / b:
print( a, b, c*10+d, e*10+f )
看起来有57种解决方案。您有六个盒子需要填充。只需运行6个成员的所有排列并检查条件:
import itertools
for a,b,c,d,e,f in itertools.permutations([0,1,2,3,4,5,6,7,8,9],6):
if a == 0 or b == 0 or c == 0 or e == 0:
continue
if (10*c + d) / a == (10*e + f) / b:
print( a, b, c*10+d, e*10+f )
看起来有57种解决方案