Erlo

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

2019-05-26 22:02:11 发布   826 浏览  
页面报错/反馈
收藏 点赞

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

效果:

 

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认