Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Math 整数的组合加起来就是一个特定的数字_Math_Combinations - Fatal编程技术网

Math 整数的组合加起来就是一个特定的数字

Math 整数的组合加起来就是一个特定的数字,math,combinations,Math,Combinations,我需要一个程序,用于所有长度为5的字母“01234”的组合,其中数字加起来等于或小于4 例子 00013, 00031, 00101, 10120 但不是 112130341 问题: 如何计算加起来等于X的字符串数 如何生成所有加起来等于X的字符串 如何生成所有加起来等于一个数字的字符串这里是一个快速而肮脏的问题。我相信你会找到办法使它更有效。代码使用数组而不是字符串 // Calculate sum of all digits func numsum(a: [Int]) -> Int {

我需要一个程序,用于所有长度为5的字母“01234”的组合,其中数字加起来等于或小于4

例子 00013, 00031, 00101, 10120

但不是 112130341

问题:

  • 如何计算加起来等于X的字符串数
  • 如何生成所有加起来等于X的字符串

  • 如何生成所有加起来等于一个数字的字符串这里是一个快速而肮脏的问题。我相信你会找到办法使它更有效。代码使用数组而不是字符串

    // Calculate sum of all digits
    func numsum(a: [Int]) -> Int {
        return reduce(a, 0, { $0 + $1 })
    }
    
    // Maximum number allowed
    let maxnum = 4
    
    // Number of digits
    let arlen = 5
    
    // Number we are looking for
    let reqnum = 4
    
    // Array that holds the combinations
    var a = Array(count: arlen, repeatedValue: 0)
    
    // Array where we keep all positive results
    var b = [[Int]]()
    
    // Function to go over all combinations and storing those that add up to the number we are looking for
    // Called recursively for each level starting with 0
    
    func addNum(level: Int = 0) {
        if level == arlen {
            return
        }
    
        a[level] = 0
        while a[level] <= maxnum {
            //println(a)
            if numsum(a) == reqnum {
                b.append(a)
            }
            else {
                addNum(level: level + 1)
            }
            a[level] += 1
        }
    }
    // Calling the function
    addNum()
    
    // Printing it out so that it looks like strings
    for arr in b {
        for n in arr {
            print(n)
        }
        println()
    }
    
    //计算所有数字之和
    func numsum(a:[Int])->Int{
    返回reduce(a,0,{$0+$1})
    }
    //允许的最大数量
    设maxnum=4
    //位数
    让arlen=5
    //我们要找的号码
    设reqnum=4
    //包含组合的数组
    变量a=数组(计数:arlen,重复值:0)
    //我们保存所有积极结果的数组
    变量b=[[Int]]()
    //函数用于检查所有组合并存储那些与我们要查找的数字相加的组合
    //对从0开始的每个级别递归调用
    func addNum(级别:Int=0){
    如果级别==arlen{
    返回
    }
    a[级别]=0
    
    而在Python中,[level]是其他Q&D解决方案

    for a in range(5):
        for b in range(5):
            for c in range(5):
                for d in range(5):
                    for e in range(5):
                        if a + b + c + d + e <= 4:
                            print a, b, c, d, e
    
    适用于范围(5)内的
    对于范围(5)内的b:
    对于范围(5)内的c:
    对于范围(5)内的d:
    对于范围(5)内的e:
    如果a+b+c+d+e(组合)是一个很好的起点,那么允许零(这需要考虑顺序)会变得复杂


    尽管零会使问题复杂化,而且你可能需要在数学堆栈上提问才能得到一个通用公式,因为组合数学可能会变得相当棘手。否则,其他答案就足以回答你的问题了。

    假设你想要所有的组合(看起来更像是问题中的排列,因为你同时列出了00013和00031)我认为首先你需要一个数字分区程序,将总和4分成几个部分,然后通过添加0将每个分区的长度扩展为5,如下所示:

    1, 1, 1, 1   ->    1, 1, 1, 1, 0
    1, 1, 2      ->    1, 1, 2, 0, 0
    1, 3         ->    1, 3, 0, 0, 0
    2, 2         ->    2, 2, 0, 0, 0
    4            ->    4, 0, 0, 0, 0
    
    然后你可以对它们中的每一个进行排列。因为数字中有许多重复,所以排列的总数并没有那么大,例如,1,1,1,0只有5个排列,11110110111011111

    关于如何做数字分区,你可以检查一下,至于排列,我是C++程序员,我会在STL库中使用NExtx排列函数,或者签出。
    这种方法非常通用,您可以处理任意和的组合,而无需进行深循环。

    我认为递归代码简短明了。它以字典顺序输出组合,无需重复工作

      procedure Combine(Digits, Sum, AMax: Integer; CurrentValue: string);
        if Digits = 0 then 
          output(CurrentValue)
        else
          for i := 0 to Min(Sum, AMax) do 
            Combine(Digits - 1, Sum - i, AMax, CurrentValue + IntToStr(i));
    
    //usage
      Combine(4, 3, 3, '');  
    
    输出:
    0000 0001 0002 0003 0010 0011 0012 0020 0021 0030 0100 0101 0102 0110 0111 0120 0200 0201 0210 0300 1000 1001 1002 1010 1011 1020 1100 1101 1110 1200 2000 2001 2010 2100
    3000

    你可以用五个嵌套的
    for
    循环,或者一个。@Edward我已经有了一个生成所有组合的解决方案。我把它放在Excel中并对它进行过滤。然后我有了解决方案,但不是一个灵活、高效的程序。一些Google给了我“sumset问题”、“同一组问题”、“数硬币问题”和“背包问题”。在我看来,我的问题相当于一个大小为4且集合为{1,1,1,2,2,2,2,3,3,3,4,4,4}的背包问题然而,背包问题被定义为找到一个有效的解决方案,而不是所有的解决方案!这是SWIFT编程语言吗?感谢这段简短的代码。然而,我所说的概念是指找到一个集合的数字,将其加到一个特定的和中。当只使用这些子集时,编程将更加有效。非常好。麻木er分区是我一直在寻找的术语。一般来说,一旦你知道什么叫分区,就很容易用谷歌搜索它。