題目:有一、二、三、4個數字,能組成多少個互不相同且無重複數字的三位數?都是多少?優化
解答:spa
(1)這是我第一次寫的
code
L = [1,2,3,4]for i1 in L: for i2 in L: if i2 != i1: for i3 in L: if i3 != i1 and i3 != i2: print("%s%s%s"%(i1,i2,i3))
(2)這是優化的blog
L = [1,2,3,4] count = 0 for i1 in L: for i2 in L: for i3 in L: if i1!=i2 and i3!=i1 and i3!=i2: count += 1 print("%d%d%d" %(i1,i2,i3)) print("一共能組成%d個" %(count))