Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找并打印每个总和为100的唯一组合,并返回1到100之间数字的所有此类组合的计数_Python_List_Recursion - Fatal编程技术网

Python 查找并打印每个总和为100的唯一组合,并返回1到100之间数字的所有此类组合的计数

Python 查找并打印每个总和为100的唯一组合,并返回1到100之间数字的所有此类组合的计数,python,list,recursion,Python,List,Recursion,数字范围为:1至100 我希望打印出1和100之间的每个唯一的组合,其总和等于100,最后打印出此类组合的计数 例如: [1,99] [1,2,97] [1,2,3,4,5,85] 所以,我需要两件事: 打印每个有效组合 返回此类组合数的最终计数 以下是我迄今为止所做的尝试,但没有成功: count = 0 def get_count(target, data_range, current_sum): global count for num in data_r

数字范围为:1至100

我希望打印出1和100之间的每个唯一的组合,其总和等于100,最后打印出此类组合的计数 例如:

[1,99]
[1,2,97]
[1,2,3,4,5,85]
所以,我需要两件事:

  • 打印每个有效组合
  • 返回此类组合数的最终计数
  • 以下是我迄今为止所做的尝试,但没有成功:

    count = 0
    def get_count(target, data_range, current_sum):    
        global count    
        for num in data_range:        
            current_sum += num    
            if current_sum > target:
                break  
            elif current_sum == target:
                count += 1    
                current_sum = 0
            elif current_sum < target: 
                get_count(target, range(num + 1, 101), current_sum)
        return count
    get_count(target = 100, data_range = range(1,101), current_sum = 0)
    
    count=0
    def get_计数(目标、数据范围、当前总和):
    全局计数
    对于数据_范围内的num:
    当前_sum+=num
    如果当前_sum>目标:
    打破
    elif current_sum==目标:
    计数+=1
    电流和=0
    elif电流和<目标:
    获取计数(目标、范围(num+1101)、当前计数和)
    返回计数
    获取计数(目标=100,数据范围=范围(1101),当前总和=0)
    
    此代码不打印组合

    def记忆(f):
    缓存={}
    def包装(*args):
    如果参数不在缓存中:
    缓存[args]=f(*args)
    返回缓存[args]
    返回包装器
    def get_计数(目标):
    @回忆
    def f(目标,当前):
    如果目标<0:返回0
    如果target==0:返回1
    范围内n的返回和(f(目标-n,n+1)(cur,目标+1))
    返回f(目标,1)
    打印(获取计数(100))
    
    I diagree这是一个完全相同的副本。它可以简化为链接问题,但在这里-你有一个额外的限制。给定的数字集是[1,2,…,100]-而不是任意的集。
    def memoized(f):
        cache = {}
        def wrapper(*args):
            if args not in cache:
                cache[args] = f(*args)
            return cache[args]
        return wrapper
    
    def get_count(target):
        @memoized
        def f(target, cur):
            if target < 0: return 0
            if target == 0: return 1
            return sum(f(target - n, n + 1) for n in range(cur, target + 1))
        return f(target, 1)
    
    print(get_count(100))