由买买提看人间百态

topics

全部话题 - 话题: fullpos
(共0页)
t*s
发帖数: 1504
1
来自主题: Programming版 - 数八皇后解法数目:python只要9行
刚刚无聊,想练练python, 就随便做了这个题。。。结果只用了9行代码,还是蛮有成
就感的
from itertools import permutations,combinations
c=0
for fullpos in permutations(range(1,9),8):
for i,j in combinations(range(1,9),2):
if abs(i-j)==abs(fullpos[i-1]-fullpos[j-1]):
break
else:
c+=1
print(c)
f*******n
发帖数: 12623
2
来自主题: Programming版 - 数八皇后解法数目:python只要9行
why not
from itertools import permutations,combinations
c=sum(all(abs(i-j)!=abs(fullpos[i-1]-fullpos[j-1]) for i,j in combinations(range(8),2)) for fullpos in permutations(range(8)))
print(c)
t*s
发帖数: 1504
3
来自主题: Programming版 - 数八皇后解法数目:python只要9行
eval(...)
one line, ha

(range(8),2)) for fullpos in permutations(range(8)))
(共0页)