即使现实总是不尽人意,也永远无法阻碍我们变得优秀。

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

方法一: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))

效果:

 

posted @ 2019-05-26 21:15  依概率收敛于你  阅读(15705)  评论(1编辑  收藏  举报