马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
怎么化简这个代码,不然不符合python洁简的风格 for a in range(1,10):
for b in range(1,10):
for c in range(1,10):
for d in range(1,10):
for e in range(1,10):
for f in range(1,10):
for g in range(1,10):
for h in range(1,10):
for i in range(1,10):
if (a + b + c) == 15 and (a + d + g ) == 15 and (a + e + i ) == 15 and (d + e + f)==15 and (g + h + i )==15:
if a != b and a != c and a != d and a != e and a != f and a != g and a != h and a != i:
if b != c and b != d and b != e and b != f and b != g and b != h and b != i :
if c != d and c != e and c != f and c != g and c != h and c != i :
if d != e and d != f and d != g and d != h and d != i :
if e != f and e != g and e != h and e != i :
if f != g and f != h and f != i :
if g != h and g != i :
if h !=i :
print(a,b,c,d,e,f,g,h,i)
本帖最后由 逃兵 于 2020-12-19 15:44 编辑
九宫格就满足几点
1.中点e=5
2.四个角为偶数acgi为偶数
又因为中点e必定为5
则有i=10-a,g=10-c
3.定一个角a时,只有两种写法(分别互换cg的值,对应影响到bdfh的值)
a一定为偶数,所以一共有2*4=8种解 angle = [[2,8],[4,6]]
e = 5
for num in range(len(angle)):
for a in angle[num]:
i = 10-a
for c in angle[num-1]:
g = 10-c
b = 15-a-c
h = 10-b
d = 15-a-g
f = 10-d
print(a,b,c,d,e,f,g,h,i)
|