1-5的所有组合之和等于<= 9

时间:2019-10-01 21:26:16

标签: python python-3.x

我需要找到总和为9的3个数字1-5的所有组合。只能使用set = [1,2,3,4,5]中的3个数字。

现在的问题是我的数字高于5。

有什么建议吗?

# arr - array to store the combination 
# index - next location in array 
# num - given number 
# reducedNum - reduced number  
def findCombinationsUtil(arr, index, num, 
                              reducedNum): 

    # Base condition 
    if (reducedNum < 0): 
        return; 

    # If combination is  
    # found, print it 
    if (reducedNum == 0): 

        for i in range(index): 
            print(arr[i], end = " "); 
        print(""); 
        return; 

    # Find the previous number stored in arr[].  
    # It helps in maintaining increasing order 
    prev = 1 if(index == 0) else arr[index - 1]; 

    # note loop starts from previous  
    # number i.e. at array location 
    # index - 1 
    for k in range(prev, num + 1): 

        # next element of array is k
        arr[index] = k; 

        # call recursively with 
        # reduced number 
        findCombinationsUtil(arr, index + 1, num,  
                                 reducedNum - k); 

# Function to find out all  
# combinations of positive numbers  
# that add upto given number. 
# It uses findCombinationsUtil()  
def findCombinations(n): 

    # array to store the combinations 
    # It can contain max n elements 
    arr = [0] * n; 


# find all combinations 
findCombinationsUtil(arr, 0, n, n);

自从我使用Python以来已经有一段时间了(从大学开始就没有),所以我有点生锈。任何反馈表示赞赏!

2 个答案:

答案 0 :(得分:2)

您可以使用一个函数来遍历给定序列中的值,并将每个值与递归调用返回的每个组合相结合,目标值减去当前值,并将目标数减少1 ,直到目标数字变为0:​​

def find_combinations(arr, target, num):
    if target >= 0 == num:
        yield []
    elif target > 0:
        for item in arr:
            for combination in find_combinations(arr, target - item, num - 1):
                yield [item, *combination]

这样:

for combination in find_combinations(range(1, 6), 9, 3):
    print(combination)

输出:

[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 1, 4]
[1, 1, 5]
[1, 2, 1]
[1, 2, 2]
[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[1, 3, 1]
[1, 3, 2]
[1, 3, 3]
[1, 3, 4]
[1, 3, 5]
[1, 4, 1]
[1, 4, 2]
[1, 4, 3]
[1, 4, 4]
[1, 5, 1]
[1, 5, 2]
[1, 5, 3]
[2, 1, 1]
[2, 1, 2]
[2, 1, 3]
[2, 1, 4]
[2, 1, 5]
[2, 2, 1]
[2, 2, 2]
[2, 2, 3]
[2, 2, 4]
[2, 2, 5]
[2, 3, 1]
[2, 3, 2]
[2, 3, 3]
[2, 3, 4]
[2, 4, 1]
[2, 4, 2]
[2, 4, 3]
[2, 5, 1]
[2, 5, 2]
[3, 1, 1]
[3, 1, 2]
[3, 1, 3]
[3, 1, 4]
[3, 1, 5]
[3, 2, 1]
[3, 2, 2]
[3, 2, 3]
[3, 2, 4]
[3, 3, 1]
[3, 3, 2]
[3, 3, 3]
[3, 4, 1]
[3, 4, 2]
[3, 5, 1]
[4, 1, 1]
[4, 1, 2]
[4, 1, 3]
[4, 1, 4]
[4, 2, 1]
[4, 2, 2]
[4, 2, 3]
[4, 3, 1]
[4, 3, 2]
[4, 4, 1]
[5, 1, 1]
[5, 1, 2]
[5, 1, 3]
[5, 2, 1]
[5, 2, 2]
[5, 3, 1]

答案 1 :(得分:2)

鉴于工作范围较小,您可以使用itertools.combinations_with_replacement施加一点蛮力。这种方法不会产生每种组合的每个排列,因此,如果需要,其他答案会为您解决(即,这种方法产生(1、3、1)而不是(1、3、1)或(3, 1,1))。

from itertools import combinations_with_replacement

combos = [x for x in combinations_with_replacement(range(1, 6), 3) if sum(x) < 10]
print(combos)
# [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 3), (1, 3, 4), (1, 3, 5), (1, 4, 4), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 2, 5), (2, 3, 3), (2, 3, 4), (3, 3, 3)]