總網頁瀏覽量

2022年2月11日 星期五

甲~己共6人,甲不排12位、乙不排23位、丙不排31位,求排法數?(2022.2.13日)

Python Code:

import itertools

w=['a','b','c','d','e','f']

num=list(set(itertools.permutations(w))) #排列並去除相同元素

other=[]

for i in range(len(num)):

    for j in range(len(num[0])):

        if num[i][0]=='a' or num[i][1]=='a' or num[i][1]=='b' or num[i][2]=='b'\

            or num[i][2]=='c' or num[i][0]=='c':

            other.append(num[i])

ans=set(num)-set(other)

print(ans)

print(len(ans))

答案:204種

Q:若原條件加上丁戊不相鄰,求排法數?

Python Code:

import itertools

w=['a','b','c','d','e','f']

num=list(set(itertools.permutations(w))) #排列並去除相同元素

other=[]

for i in range(len(num)):

    for j in range(len(num[0])):

        if num[i][0]=='a' or num[i][1]=='a' or num[i][1]=='b' or \

            num[i][2]=='b'or num[i][2]=='c' or num[i][0]=='c':

            other.append(num[i])

preans=set(num)-set(other)

preans=list(preans)

another=[]

for i in range(len(preans)):

    for j in range(0,len(preans[0])-1):

        if (preans[i][j]=='d' and preans[i][j+1]=='e') or \

            (preans[i][j]=='e' and preans[i][j+1]=='d'):

                another.append(preans[i])

ans=set(preans)-set(another)

print(ans)

print(len(ans))

答案:128種


沒有留言:

張貼留言