Skip to content

Instantly share code, notes, and snippets.

@IanSmith123
Created August 19, 2017 11:04
Show Gist options
  • Save IanSmith123/72b82a6e05f80217893c6c6697b15ebf to your computer and use it in GitHub Desktop.
Save IanSmith123/72b82a6e05f80217893c6c6697b15ebf to your computer and use it in GitHub Desktop.
计算1-9不重复填入九宫格,横竖斜相加和相等
count = 0
for i in range(1, 10):
for j in range(1, 10):
a = [i, j, 15-i-j]
if len(set(a)) < 3 or 15-i-j<1:
count += 1
continue
for k in range(1, 10):
for l in range(1, 10):
b = [k, l, 15-k-l]
if len(set(b)) < 3 or 15-k-l<1:
count += 1
continue
for m in range(1, 10):
for n in range(1, 10):
c = [m, n, 15-m-n]
if len(set(c)) < 3 or 15-m-n<1:
count += 1
continue
count += 1
# if a[0]+b[0]+c[0] ==15 and a[1]+b[1]+c[1]==15 and a[2]+b[2]+c[2]==15 and len(set(a) & set(b))+ len(set(a) & set(c)) + len(set(b) & set(c)) == 0:
if a[0]+b[0]+c[0] ==15 and a[1]+b[1]+c[1]==15 and a[2]+b[2]+c[2]==15 and len(set(a) & set(b))+ len(set(a) & set(c)) + len(set(b) & set(c)) == 0\
and a[0]+b[1]+c[2]==15 and a[2]+b[1]+c[0]==15:
print("*"*15)
print(a)
print(b)
print(c)
print(count)
@IanSmith123
Copy link
Author

IanSmith123 commented Aug 19, 2017

O----O----O
|    |    |
O----O----O
|    |    |
O----O----O

@IanSmith123
Copy link
Author

依次填入1-9的数使得横竖相加和为15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment