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))

效果:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM