码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-03 19:54:32      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:tin   组成   排序   ted   red   多少   not   合成   add   

"""
题目:有 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 个数字,能组成多少个互不相同且无重复数字的三位数?都是多 少?

标签:tin   组成   排序   ted   red   多少   not   合成   add   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!