t*s 发帖数: 1504 | 1 刚刚无聊,想练练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 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 eval(...)
one line, ha
(range(8),2)) for fullpos in permutations(range(8))) |
|