首页 > 其他 > 详细

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

时间:2017-12-03 19:32:20      阅读:337      评论:0      收藏:0      [点我收藏+]
"""
题目:有 1、2、3、4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多
少? 
"""
# 解决思路:三个for循环依次从4个数中取1个 把所有情况都包括  利用集合来去重


sample_list = [1, 2, 3, 4]

result =set()
# 第一层循环取百位数
for hundreds in range(len(sample_list)):

    # 第二层循环取十位数
    for decimal in range(len(sample_list)):

        # 第三层循环取个位数
        for unit in range(len(sample_list)):

            # 组合成一个三位数
            temp = sample_list[hundreds] * 100 + sample_list[decimal] * 10 + sample_list[unit] * 1

            # 判断集合中是否已存在 不存在则加入到集合
            if temp not in result:
                result.add(temp)
            else:
                continue    

# 展示最终结果                
print("满足条件的三位数总共有{}个。".format(len(result)))

# 转成list 排序后循环遍历  展示结果
index = 0   
for n in sorted(list(result)):
    index += 1
    print("第{}个是:{}".format(index, n))

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

原文:http://www.cnblogs.com/endurance9/p/7966867.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!