python百炼成钢实例001-数字组合

实例001:数字组合

需求

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

程序分析 遍历全部可能,把有重复的剃掉。

# 题目:有1、2、3、4个数字,能组成多少个相互不同且无重复的三位数?都是多少?
num = 0
threeNum = []
for a in range(1,5):
    for b in range(1,5):
        for c in range(1,5):
            if (a !=b) and (a!=c) and (b != c):
                abc = a*100+b*10+c
                print(abc)
                threeNum.append(abc)
                num +=1
print("有1、2、3、4个数字,能组成{}个相互不同且无重复的三位数,分别为{}".format(num,threeNum))

输出结果

>> D:Esoftpython.exe D:/E/soft/pythoncode/pythonWork/Case1_10/01ThreeNum.py
>> 有1、
2、3、4个数字,能组成24个相互不同且无重复的三位数,分别为[123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]

注意点:

  • 1、去重
  • 2、数字和字符串拼接区分
  • 3、列表存储
原文地址:https://www.cnblogs.com/shishibuwan/p/15513531.html