2018-09-18 滴滴笔试 排列小球

三色球,相同颜色不相邻,排列个数。


2018-09-18 滴滴笔试 排列小球_第1张图片
image.png
def fun(l1_,l2_):
    l1 = [[i[0],i[1]] for i in l1_]
    l2 = [i for i in l2_]
    global result
    if l1[0][1] == 0 and l1[1][1] == 0 and l1[2][1] == 0:
        result += 1
        print(l2)
        return 1
    for i in range(3):
        tu = l1[i]
        if l1[i][1]!=0:
            if len(l2) == 0:
                l2.append(l1[i][0])
                l1[i][1] -= 1
                fun(l1, l2)
            elif l2[-1] != l1[i][0]:
                l2.append(l1[i][0])
                l1[i][1] -= 1
                fun(l1, l2)
            else:
                continue


nPQR = input()
np = int(nPQR.split(" ")[0])
nq = int(nPQR.split(" ")[1])
nr = int(nPQR.split(" ")[2])

l1= [["P" , np], ["Q" , nq], ["R" , nr] ]
l2 = []

result = 0

fun(l1,l2)
print(result)

你可能感兴趣的:(2018-09-18 滴滴笔试 排列小球)