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

方法1:

Python 3.X 练习集100题 01
import itertools
from functools import reduce

lyst = [1, 2, 3, 4, 5]
result = []

for el in itertools.permutations(lyst, 3):
    temp = reduce(lambda x,y:x * 10 +y, el)
    result.append(temp)

print(f'总共有{len(result)}个互不相同且五重复数字的三位数,分别是:')
print(result)
Python 3.X 练习集100题 01

方法2:

Python 3.X 练习集100题 01
import copy
numbers = [1, 2, 3, 4, 5]
tri = []
for i in numbers:
    d_u = copy.copy(numbers)
    d_u.remove(i)
    for j in d_u:
        u = copy.copy(d_u)
        u.remove(j)
        for k in u :
            tri.append(i * 100 + j * 10 + k)

print(tri)
print(len(tri))
Python 3.X 练习集100题 01

方法3:

Python 3.X 练习集100题 01
import itertools
count = 0
for i in itertools. product([1,2,3,4,5], repeat=3):
    if i[0] != i[1] and i[0] != i[2] and i[1] != i[2]:
        print(i[0]*100 + i[1]*10 + i[2])
        count += 1
print(count)
Python 3.X 练习集100题 01

方法4:

Python 3.X 练习集100题 01
firstNumber = [1, 2, 3, 4, 5]
count = 0
for i in firstNumber:
    secondNumber = firstNumber[:]
    secondNumber.remove(i)
    for j in secondNumber:
        thirdNumber = secondNumber[:]
        thirdNumber.remove(j)
        for k in thirdNumber:
            print(str(i) + str(j) + str(k), end=",")
            count += 1
print("一共有{}个数".format(count))
Python 3.X 练习集100题 01

相关文章:

  • 2022-12-23
  • 2021-12-02
  • 2021-10-14
  • 2021-05-16
  • 2022-12-23
  • 2022-01-07
  • 2021-10-25
  • 2021-03-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2022-02-10
  • 2022-12-23
相关资源
相似解决方案