1至9九个数字,横竖都有3个格,思考怎么使每行、每列和对角线上的三数之和都等于15
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
list0 = [] # 除了中间那个数是5,周围的是别的数
for i in range(1, 10):
if i == 5:
continue
list0.append(i)
'''
假设第一个数是x,第二个是y,那么这9个数应该是:
x y 15-x-y
20-2x-y 5 2x+y-10
x+y-5 10-y 10-x
'''
count = 0
for x in list0:
list_temp = list0[:] # 复制
list_temp.remove(x) # 去除x后的列表
for y in list_temp:
set0 = set([x, y, 15 - x - y, 20 - 2 * x - y, 5, 2 * x + y - 10, x + y - 5, 10 - y, 10 - x])
if (15-x-y) in list0 and (20-2*x-y) in list0 and (2*x+y-10) in list0 and (x+y-5) in list0 and (10-y) in list0 and (10-x) in list0 and len(set0) == 9:
count += 1
print("第%s