Algorithm 打印n个整数求和的所有方法,以便它们求和。

Algorithm 打印n个整数求和的所有方法,以便它们求和。,algorithm,math,Algorithm,Math,我正试图想出一个算法,它将打印出所有可能的方法来求N个整数的和,这样它们就可以得到一个给定的值 例如。打印4个整数之和的所有方法,使其总和为5 结果应该是: 5 0 0 0 4 1 0 0 3 2 0 0 3 1 1 0 2 3 0 0 2 2 1 0 2 1 2 0 2 1 1 1 1 4 0 0 1 3 1 0 1 2 2 0 1 2 1 1 1 1 3 0 1 1 2 1 1 1 1 2 我还没有测试过这个: procedure allSum (int tot, int n, int

我正试图想出一个算法,它将打印出所有可能的方法来求N个整数的和,这样它们就可以得到一个给定的值

例如。打印4个整数之和的所有方法,使其总和为5

结果应该是:

5 0 0 0
4 1 0 0
3 2 0 0
3 1 1 0
2 3 0 0
2 2 1 0
2 1 2 0
2 1 1 1
1 4 0 0
1 3 1 0 
1 2 2 0
1 2 1 1
1 1 3 0
1 1 2 1
1 1 1 2
我还没有测试过这个:

procedure allSum (int tot, int n, int desiredTotal) return int if n > 0 int i = for (int i = tot; i>=0; i--) { push i onto stack; allSum(tot-i, n-1, desiredTotal); pop top of stack } else if n==0 if stack sums to desiredTotal then print the stack end if end if 过程allSum(inttot,intn,intdesiredtotal)返回int 如果n>0 int i= 对于(int i=tot;i>=0;i--){ 将i推到堆栈上; 全部金额(总金额,n-1,期望总金额); 从栈顶弹出 } 如果n==0,则为else 如果堆栈总和达到所需的总和,则在以下情况下打印堆栈结束 如果结束 我相信有更好的办法

我还没有测试过这个:

procedure allSum (int tot, int n, int desiredTotal) return int if n > 0 int i = for (int i = tot; i>=0; i--) { push i onto stack; allSum(tot-i, n-1, desiredTotal); pop top of stack } else if n==0 if stack sums to desiredTotal then print the stack end if end if 过程allSum(inttot,intn,intdesiredtotal)返回int 如果n>0 int i= 对于(int i=tot;i>=0;i--){ 将i推到堆栈上; 全部金额(总金额,n-1,期望总金额); 从栈顶弹出 } 如果n==0,则为else 如果堆栈总和达到所需的总和,则在以下情况下打印堆栈结束 如果结束
我相信有更好的办法

解决这个问题的关键是递归。这里有一个python的工作实现。它打印出所有可能的排列,这些排列的总和就是总数。您可能希望消除重复的组合,可能需要使用一些集合或散列机制来过滤它们

def sum(n, value):
    arr = [0]*n  # create an array of size n, filled with zeroes
    sumRecursive(n, value, 0, n, arr);

def sumRecursive(n, value, sumSoFar, topLevel, arr):
    if n == 1:
        if sumSoFar > value:
            return False
        else:
            for i in range(value+1): # i = 0...value
                if (sumSoFar + i) == value:
                    arr[(-1*n)] = i # put i in the n_th last index of arr
                    print arr;
                    return True

    else:
        for i in range(value+1): # i = 0...value
            arr[(-1*n)] = i  # put i in the n_th last index of arr
            if sumRecursive(n-1, value, sumSoFar + i, topLevel, arr):
                if (n == topLevel):
                    print "\n"

通过一些额外的努力,这可能会被简化,以消除我传递给递归函数的一些参数。正如redcayuga的伪代码所建议的,使用堆栈而不是手动管理数组也是一个更好的主意。

解决问题的关键是递归。这里有一个python的工作实现。它打印出所有可能的排列,这些排列的总和就是总数。您可能希望消除重复的组合,可能需要使用一些集合或散列机制来过滤它们

def sum(n, value):
    arr = [0]*n  # create an array of size n, filled with zeroes
    sumRecursive(n, value, 0, n, arr);

def sumRecursive(n, value, sumSoFar, topLevel, arr):
    if n == 1:
        if sumSoFar > value:
            return False
        else:
            for i in range(value+1): # i = 0...value
                if (sumSoFar + i) == value:
                    arr[(-1*n)] = i # put i in the n_th last index of arr
                    print arr;
                    return True

    else:
        for i in range(value+1): # i = 0...value
            arr[(-1*n)] = i  # put i in the n_th last index of arr
            if sumRecursive(n-1, value, sumSoFar + i, topLevel, arr):
                if (n == topLevel):
                    print "\n"

通过一些额外的努力,这可能会被简化,以消除我传递给递归函数的一些参数。正如redcayuga的伪代码所建议的,使用堆栈而不是手动管理数组也是一个更好的主意。

这是基于Alinium的代码。
我对它进行了修改,这样它就能打印出所有可能的组合,因为他已经完成了所有的排列。
另外,当n=1时,我认为不需要for循环,因为在这种情况下,只有一个数字应该使总和相等。
各种其他修改,以使边界情况正常工作

def sum(n, value):
    arr = [0]*n  # create an array of size n, filled with zeroes
    sumRecursive(n, value, 0, n, arr);

def sumRecursive(n, value, sumSoFar, topLevel, arr):
    if n == 1:
        if sumSoFar <= value:
            #Make sure it's in ascending order (or only level)
            if topLevel == 1 or (value - sumSoFar >= arr[-2]):
                arr[(-1)] = value - sumSoFar #put it in the n_th last index of arr
                print arr
    elif n > 0:
        #Make sure it's in ascending order
        start = 0
        if (n != topLevel):
            start = arr[(-1*n)-1]   #the value before this element

        for i in range(start, value+1): # i = start...value
            arr[(-1*n)] = i  # put i in the n_th last index of arr
            sumRecursive(n-1, value, sumSoFar + i, topLevel, arr)
def和(n,值):
arr=[0]*n#创建一个大小为n的数组,用零填充
sumRecursive(n,value,0,n,arr);
def SUM递归(n,值,sumSoFar,顶级,arr):
如果n==1:

如果sumSoFar这是基于Alinium的代码。
我对它进行了修改,这样它就能打印出所有可能的组合,因为他已经完成了所有的排列。
另外,当n=1时,我认为不需要for循环,因为在这种情况下,只有一个数字应该使总和相等。
各种其他修改,以使边界情况正常工作

def sum(n, value):
    arr = [0]*n  # create an array of size n, filled with zeroes
    sumRecursive(n, value, 0, n, arr);

def sumRecursive(n, value, sumSoFar, topLevel, arr):
    if n == 1:
        if sumSoFar <= value:
            #Make sure it's in ascending order (or only level)
            if topLevel == 1 or (value - sumSoFar >= arr[-2]):
                arr[(-1)] = value - sumSoFar #put it in the n_th last index of arr
                print arr
    elif n > 0:
        #Make sure it's in ascending order
        start = 0
        if (n != topLevel):
            start = arr[(-1*n)-1]   #the value before this element

        for i in range(start, value+1): # i = start...value
            arr[(-1*n)] = i  # put i in the n_th last index of arr
            sumRecursive(n-1, value, sumSoFar + i, topLevel, arr)
def和(n,值):
arr=[0]*n#创建一个大小为n的数组,用零填充
sumRecursive(n,value,0,n,arr);
def SUM递归(n,值,sumSoFar,顶级,arr):
如果n==1:

如果在纯数学中sumSoFar,那么求整数和得到给定总数的方法称为分区。如果你在谷歌上搜索“整数分区”,有很多信息。您正在寻找具有特定数量元素的整数分区。我相信你可以采取一种已知的生成机制,并适应这种额外的条件。维基百科对这个话题有很好的概述。Mathematica甚至有一个函数可以做你想做的事情:
整数部分[5,4]

在纯数学中,一种求整数和得到给定总数的方法称为分区。如果你在谷歌上搜索“整数分区”,有很多信息。您正在寻找具有特定数量元素的整数分区。我相信你可以采取一种已知的生成机制,并适应这种额外的条件。维基百科对这个话题有很好的概述。Mathematica甚至有一个函数可以实现您想要的功能:
IntegerPartitions[5,4]

我找到了一种基于Alinium代码的领域规范的ruby方法

class Domain_partition

    attr_reader :results,
                :domain,
                :sum,
                :size

    def initialize(_dom, _size, _sum)
        _dom.is_a?(Array) ? @domain=_dom.sort : @domain= _dom.to_a
        @results, @sum, @size = [], _sum, _size
        arr = [0]*size  # create an array of size n, filled with zeroes
        sumRecursive(size, 0, arr)
    end

    def sumRecursive(n, sumSoFar, arr)

        if n == 1
            #Make sure it's in ascending order (or only level)
            if sum - sumSoFar >= arr[-2] and @domain.include?(sum - sumSoFar)
                final_arr=Array.new(arr)
                final_arr[(-1)] = sum - sumSoFar #put it in the n_th last index of arr
                @results<<final_arr
            end

        elsif n > 1

            #********* dom_selector ********

            n != size ? start = arr[(-1*n)-1] : start = domain[0]
            dom_bounds=(start*(n-1)..domain.last*(n-1))

            restricted_dom=domain.select do |x|

                if x < start 
                    false; next
                end

                if size-n > 0
                    if dom_bounds.cover? sum-(arr.first(size-n).inject(:+)+x) then true
                    else false end  
                else 
                    dom_bounds.cover?(sum+x) ? true : false
                end
            end # ***************************

            for i in restricted_dom
                _arr=Array.new(arr)
                _arr[(-1*n)] = i 
                sumRecursive(n-1, sumSoFar + i, _arr)
            end
        end
    end
end 

a=Domain_partition.new (-6..6),10,0 
p a

b=Domain_partition.new [-4,-2,-1,1,2,3],10,0 
p b
类域分区
属性读取器:结果,
:域名,
:sum,
:尺寸
定义初始化(_dom、_size、_sum)
_dom.u是?(数组)吗@domain=\u dom.sort:@domain=\u dom.to\u a
@结果,@sum,@size=[],\u sum,\u size
arr=[0]*size#创建一个大小为n的数组,用零填充
sumRecursive(大小、0、arr)
结束
def sumSoFar(北区、苏姆索法尔区、arr区)
如果n==1
#确保它是按升序排列的(或仅按级别排列)
如果sum-sumSoFar>=arr[-2]和@domain.include?(sum-sumSoFar)
final_arr=Array.new(arr)
final_arr[(-1)]=sum-sumSoFar#将其放入arr的第n个最后索引中
@结果0
如果dom_.cover?求和-(arr.first(size-n)。注入(:+)+x)然后为真
否则就错了
其他的
dom_界限覆盖?(总和+x)?对:错
结束
完#***************************
因为我在禁区
_arr=数组。新建(arr)
_arr[-1*n]=i
sumSoFar(n-1,sumSoFar+i,_arr)
结束
结束
结束
结束
a=域分区。新(-6..6),10,0
通讯社
b=域_分区。新[-4,-2,-1,1,2,3],10,0
p b

我找到了一种基于Alinium代码的ruby域规范

class Domain_partition

    attr_reader :results,
                :domain,
                :sum,
                :size

    def initialize(_dom, _size, _sum)
        _dom.is_a?(Array) ? @domain=_dom.sort : @domain= _dom.to_a
        @results, @sum, @size = [], _sum, _size
        arr = [0]*size  # create an array of size n, filled with zeroes
        sumRecursive(size, 0, arr)
    end

    def sumRecursive(n, sumSoFar, arr)

        if n == 1
            #Make sure it's in ascending order (or only level)
            if sum - sumSoFar >= arr[-2] and @domain.include?(sum - sumSoFar)
                final_arr=Array.new(arr)
                final_arr[(-1)] = sum - sumSoFar #put it in the n_th last index of arr
                @results<<final_arr
            end

        elsif n > 1

            #********* dom_selector ********

            n != size ? start = arr[(-1*n)-1] : start = domain[0]
            dom_bounds=(start*(n-1)..domain.last*(n-1))

            restricted_dom=domain.select do |x|

                if x < start 
                    false; next
                end

                if size-n > 0
                    if dom_bounds.cover? sum-(arr.first(size-n).inject(:+)+x) then true
                    else false end  
                else 
                    dom_bounds.cover?(sum+x) ? true : false
                end
            end # ***************************

            for i in restricted_dom
                _arr=Array.new(arr)
                _arr[(-1*n)] = i 
                sumRecursive(n-1, sumSoFar + i, _arr)
            end
        end
    end
end 

a=Domain_partition.new (-6..6),10,0 
p a

b=Domain_partition.new [-4,-2,-1,1,2,3],10,0 
p b
类域分区
属性读取器:结果,