python3 题目 有四个数字:1、2

2020-01-16 16:14:42 浏览数 (1)

方法一:for循环遍历

代码语言:javascript复制
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即可

代码语言:javascript复制
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))

效果:

0 人点赞