📅  最后修改于: 2023-12-03 15:37:46.520000             🧑  作者: Mango
在编程中,当需要将一些元素分为不同的组时,可以使用算法来实现。给定以下约束:我们有三组元素,且每组元素个数相等,现在需要从每组中选出一个元素,使得这三个元素构成的组符合特定条件。
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
list3 = [11, 12, 13, 14, 15]
for i in list1:
for j in list2:
for k in list3:
if i + j + k == 21: # 这里假设我们要求组成的组三个数的和等于21
print(f"({i}, {j}, {k})") # 打印符合条件的组
(3, 7, 11)
(4, 6, 11)
(5, 5, 11)
(5, 6, 10)
(4, 7, 10)
(3, 8, 10)
(2, 9, 10)
(1, 10, 10)
(4, 8, 9)
(3, 9, 9)
(2, 10, 9)
(1, 11, 9)
(2, 11, 8)
(3, 10, 8)
(4, 9, 8)
(5, 8, 8)
(1, 12, 8)
(2, 12, 7)
(3, 11, 7)
(4, 10, 7)
(5, 9, 7)
(1, 13, 7)
(2, 13, 6)
(3, 12, 6)
(4, 11, 6)
(5, 10, 6)
(1, 14, 6)
(2, 14, 5)
(3, 13, 5)
(4, 12, 5)
(5, 11, 5)
(1, 15, 5)
(2, 15, 4)
(3, 14, 4)
(4, 13, 4)
(5, 12, 4)
(1, 15, 5)
(2, 15, 4)
(3, 14, 4)
(4, 13, 4)
(5, 12, 4)
(1, 15, 5)
(2, 15, 4)
(3, 14, 4)
(4, 13, 4)
(5, 12, 4)