python3 题目 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

时间:2024-04-25 03:52:51

方法一:for循环遍历

counter=0
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if i !=j and j !=k and k !=i:
print("{}{}{}".format(i,j,k),end=" ")
counter +=1
print("")
print("共{}种组合".format(counter))

方法二:用itertools中的permutations即可

counter=0
from itertools import permutations
for i in permutations([1,2,3,4],3):
print("{}{}{}".format(i[0],i[1],i[2]),end=" ")
counter +=1
print("")
print("共{}种组合".format(counter))

效果:

python3 题目 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?