Home >Backend Development >PHP Tutorial >Find all combinations that sum to n
Given a number n
Requirements:
(1) The value of the integer on the left side of the equation is 1~n-1.
(2) It is required that the sum of the left side of the equation is n.
<code>若 n = 3; 1 + 1 + 1 = 3; 1 + 2 = 3;</code>
Given a number n
Requirements:
(1) The value of the integer on the left side of the equation is 1~n-1.
(2) It is required that the sum of the left side of the equation is n.
<code>若 n = 3; 1 + 1 + 1 = 3; 1 + 2 = 3;</code>
The poster can go and learn the general function
This should be a template question for the parent function
<code>function calcN (n) { var res = [], cache = {}; loop(n); function loop(k, arr) { arr = arr || []; var i = 1, count = k / 2 | 0; cache[k] = true; while (i </code>
For C++, just modify it based on the above, use map
and vector