题目描述
将编号为1~10的10本书排放在书架上,要求编号相邻的书不能放在相邻的位置。
请计算一共有多少种不同的排列方案。
注意,需要提交的是一个整数,不要填写任何多余的内容。
分析:将所有情况列出,排除编号相邻图书相邻的情况
import itertools
book = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum = 0
for i in itertools.permutations(book, 10):
if abs(i[0]-i[1]) != 1 and abs(i[1]-i[2]) != 1 and abs(i[2]-i[3]) != 1 and abs(i[3]-i[4]) != 1 and abs(i[4]-i[5]) != 1 and abs(i[5]-i[6]) != 1 and abs(i[6]-i[7]) != 1 and abs(i[7]-i[8]) != 1 and abs(i[8]-i[9]) != 1:
sum += 1
print(sum)
结果:479306