N개의 숫자가 있고, 그 중 M개의 조합을 더하면 숫자 X가 됩니다. 이 M 번호가 어떤 숫자의 조합인지 요청하세요. M<=N이고 M의 각 조합에 있는 숫자는 한 번만 나타납니다.——Python

【설명】:

N개의 숫자가 있고, 그 중 M개의 조합을 더하면 숫자 X가 됩니다. 이 M 번호가 어떤 숫자의 조합인지 요청하세요. 그 중 M<=N이고, M에서 각 조합의 숫자는 한 번만 나타난다.

예: N개의 숫자가 있습니다: [1, 2, 3, 4, 5, 6, 7, 8, 9]

필수 개수 X: 12

M, 즉 가능한 모든 숫자 조합(예: [3,9], [4,8], [5,7], [1,4,9]...)을 찾습니다.

 

Python으로 구현됨:

# 提供一个数组和目标的和
def solve(arr, target):
    # 去重并排序
    arr = list(set(arr))
    arr.sort()
    if target in arr:
        print('1 : ',target)
    for i in range(2, len(arr) + 1, 1):
        resultArr = []              # 指定最终的数组用来存放该轮的结果(利用数组参数传的是数组的引用,最终结果数组是作为返回值来使用的
        action(arr, 0, target, [], i, resultArr)
        if len(resultArr):
            print(i, resultArr)

# 整个数组、当前开始的索引、目标总额、当前的数组、需要多少个数、最终结果数组
def action(arr, startIndex, targetVal, curArr, totalNum, resArr):
    tempIndex = startIndex
    curArrLen = len(curArr)             # 当前结果数组的长度
    sumArr = sum(curArr)                # 当前结果数组的总和

    if startIndex == 0:                 # 第一次
        # 下一个索引值存在,且当前数组中索引
        while(len(arr) > tempIndex + 1 and arr[tempIndex] + arr[tempIndex + 1] + sumArr <= targetVal):
            temparr = []
            temparr.append(arr[tempIndex])
            action(arr, tempIndex + 1, targetVal, temparr, totalNum, resArr)
            tempIndex += 1

    else:
        # 只差一个数就满足条件
        if curArrLen == totalNum - 1:
            # 直接看那个所需要的数是否在数组中当前索引后面的范围内
            contain = (targetVal - sumArr) in arr
            if contain and arr.index(targetVal - sumArr) >= tempIndex:
                # 如果数组中有该值,且是当前索引,或在其后,将该值加入到结果数组中,并将结果数组加到最终数组中
                curArr.append(targetVal - sumArr)
                resArr.append(curArr)
            
        # 仍差大于一个以上的数
        elif (curArrLen < (totalNum - 1)):
            while(len(arr) > tempIndex + 1 and arr[tempIndex] + arr[tempIndex + 1] + sumArr <= targetVal):
                t = [e for e in curArr]
                t.append(arr[tempIndex])
                action(arr, tempIndex + 1, targetVal, t, totalNum, resArr)
                tempIndex += 1

간단한 테스트:

arr = [1,2,3,4,5,6,7,8,9]
target = 12
solve(arr, target)

결과:

 

추천

출처blog.csdn.net/hao_13/article/details/130468684