有没有一个函数可以计算数组中所有可能的数字之间的总和?

huangapple go评论61阅读模式
英文:

Is there a function to calculate all possible sum of an array of number between them?

问题

I am currently facing a problem for a few days already.
I have a value, say N = 0.8
And an array of numbers, say tab = [0.4, 0.1, 0.4, 0.5]
Here we can see that the values at index 0 and 2 are 0.8, but I can't program this in a clean way, I end up with too many for loops (3 to be precise).

I tried to make a naive algorithm.
Create a variable value = 0
Two for loops, add the first element in the first loop and the others in the second one while checking that value + tab[j] <= 0.8, in some cases it works and in others, it's less than 0.8.

英文:

I am currently facing a problem for a few days already.
I have a value, say N = 0.8
And an array of numbers, say tab = [0.4, 0.1, 0.4, 0.5]
Here we can see that the values at index 0 and 2 are 0.8, but I can't program this in a clean way, I end up with too many for loops (3 to be precise).

I tried to make a naive algorithm.
Create a variable value = 0
Two for loops, add the first element in the first loop and the others in the second one while checking that value + tab[j] &lt;= 0.8, in some cases it works and in others, it's less than 0.8.

答案1

得分: 0

以下是翻译好的部分:

"Here's a recursive version." -> "这是一个递归版本。"
"We have base cases when the total is zero, and we return true, when the list of values is empty, and we return false. If the first number in the list is too large, we drop it and recur. Then we try using the first number, subtracting it from the target and recurring with the remaining numbers. If that doesn't work, we try dropping the first number and recurring with the same target." -> "当总和为零时,我们有基本情况,返回true;当值的列表为空时,返回false。如果列表中的第一个数字太大,我们会将其丢弃并进行递归。然后,我们尝试使用第一个数字,从目标中减去它并使用剩余的数字进行递归。如果这不起作用,我们尝试丢弃第一个数字并使用相同的目标进行递归。"

"const canSum = (t, [n, ...ns]) =>\n t == 0\n ? true\n : n === undefined\n ? false\n : n > t\n ? canSum (t, ns)\n : canSum (t - n, ns) || canSum (t, ns)" -> "const canSum = (t, [n, ...ns]) =>\n t == 0\n ? true\n : n === undefined\n ? false\n : n > t\n ? canSum (t, ns)\n : canSum (t - n, ns) || canSum (t, ns)"

"console .log (canSum (0.8, [0.4, 0.1, 0.4, 0.5]) //=> ${canSum (0.8, [0.4, 0.1, 0.4, 0.5])})" -> "console .log (canSum (0.8, [0.4, 0.1, 0.4, 0.5]) //=> ${canSum (0.8, [0.4, 0.1, 0.4, 0.5]}"

"console .log (' ')" -> "console .log (' ')"
"console .log ('Trying 1 - 40 with [2, 3, 5, 7, 11, 13]')" -> "console .log ('尝试 1 - 40 与 [2, 3, 5, 7, 11, 13]')"
"console .log ('=======================================')" -> "console .log ('=======================================')"

"Array.from ({length: 40}, (, i) => i + 1)\n .forEach (n => console .log ([2, 3, 5, 7, 11, 13] ${canSum (n, [2, 3, 5, 7, 11, 13]) ? '*can*' : '*cannot*'} sum to ${n}))" -> "Array.from ({length: 40}, (, i) => i + 1)\n .forEach (n => console .log ([2, 3, 5, 7, 11, 13] ${canSum (n, [2, 3, 5, 7, 11, 13]) ? '*can*' : '*cannot*'} sum to ${n}))"

"We can write something similar to find the first set that works:" -> "我们可以编写类似的代码来查找第一个有效的集合:"

"const findSummands = (\n t, [n, ...ns], \n u = t > 0 && n != undefined && findSummands (t, ns), \n v = t > 0 && n != undefined && findSummands (t - n, ns)\n) => t == 0 ? [] : n === undefined ? undefined : u ? u : (v ? [n, ...v] : undefined)" -> "const findSummands = (\n t, [n, ...ns], \n u = t > 0 && n != undefined && findSummands (t, ns), \n v = t > 0 && n != undefined && findSummands (t - n, ns)\n) => t == 0 ? [] : n === undefined ? undefined : u ? u : (v ? [n, ...v] : undefined)"

"console .log (findSummands (0.8, [0.4, 0.1, 0.4, 0.5]) //=> [${findSummands (0.8, [0.4, 0.1, 0.4, 0.5]) .join(', ')}])" -> "console .log (findSummands (0.8, [0.4, 0.1, 0.4, 0.5]) //=> [${findSummands (0.8, [0.4, 0.1, 0.4, 0.5]).join(', ')}]"

"console .log (' ')" -> "console .log (' ')"
"console .log ('Trying 1 - 40 with [2, 3, 5, 7, 11, 13]')" -> "console .log ('尝试 1 - 40 与 [2, 3, 5, 7, 11, 13]')"
"console .log ('=======================================')" -> "console .log ('=======================================')"

"Array.from ({length: 40}, (, i) => i + 1) .forEach (n => {\n const summands = findSummands (n, [2, 3, 5, 7, 11, 13])\n console .log (\n summands ? ${n} = ${summands .join (' + ')} : ${n} (no answer found)\n )\n})" -> "Array.from ({length: 40}, (, i) => i + 1) .forEach (n => {\n const summands = findSummands (n, [2, 3, 5, 7, 11, 13])\n console .log (\n summands ? ${n} = ${summands .join (' + ')} : ${n} (no answer found)\n )\n})"

英文:

Here's a recursive version.

We have base cases when the total is zero, and we return true, when the list of values is empty, and we return false. If the first number in the list is too large, we drop it and recur. Then we try using the the first number, subtracting it from the target and recurring with the remaining numbers. If that doesn't work, we try dropping the first number and recurring with the same target.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const canSum = (t, [n, ...ns]) =&gt;
  t == 0
    ? true
  : n === undefined
    ? false
  : n &gt; t
    ? canSum (t, ns)
  : canSum (t - n, ns) || canSum (t, ns)

console .log (`canSum (0.8, [0.4, 0.1, 0.4, 0.5]) //=&gt; ${canSum (0.8, [0.4, 0.1, 0.4, 0.5])}`)

console .log (&#39; &#39;)
console .log (`Trying 1 - 40 with [2, 3, 5, 7, 11, 13]`)
console .log (`=======================================`)

Array.from ({length: 40}, (_, i) =&gt; i + 1) 
  .forEach (n =&gt; console .log (`[2, 3, 5, 7, 11, 13] ${canSum (n, [2, 3, 5, 7, 11, 13]) ? &#39;*can*&#39; : &#39;*cannot*&#39;} sum to ${n}`))

<!-- language: lang-css -->

.as-console-wrapper {max-height: 100% !important; top: 0}

<!-- end snippet -->

We can write something similar to find the first set that works:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const findSummands = (
  t, [n, ...ns], 
  u = t &gt; 0 &amp;&amp; n != undefined &amp;&amp; findSummands (t, ns), 
  v = t &gt; 0 &amp;&amp; n != undefined &amp;&amp; findSummands (t - n, ns)
) =&gt; t == 0 ? [] : n === undefined ? undefined : u ? u : (v ? [n, ...v] : undefined)

console .log (`findSummands (0.8, [0.4, 0.1, 0.4, 0.5]) //=&gt; [${findSummands (0.8, [0.4, 0.1, 0.4, 0.5]) .join(&#39;, &#39;)}]`)

console .log (&#39; &#39;)
console .log (`Trying 1 - 40 with [2, 3, 5, 7, 11, 13]`)
console .log (`=======================================`)

Array.from ({length: 40}, (_, i) =&gt; i + 1) .forEach (n =&gt; {
  const summands = findSummands (n, [2, 3, 5, 7, 11, 13])
  console .log (
    summands ? `${n} = ${summands .join (&#39; + &#39;)}` : `${n}  (no answer found)`
  )
})

<!-- language: lang-css -->

.as-console-wrapper {max-height: 100% !important; top: 0}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 08:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定