Hello, the task was set: it is necessary to find the summands of the numbers in a given quantity, so that one array of the items forms the maximum available number, and the second is the minimum. There is also a limitation that the terms cannot be numbers greater than 9. That is, it is necessary to split the incoming number into digits, the sum of which will give this number, provided that the specified number of terms will always be such that it is possible to collect a number from this number of digits.
Closed due to the fact that the essence of the question is not clear to the participants Harry , vikttur , jfs , D-side , Kromster 30 Mar '18 at 14:09 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- Do you need ALL such arrays? If the first one is found, it is 12 units :) - Harry
- So, excuse me, I pass, now I don’t understand anything at all ...: (I hope that there will be a more intelligent one. - Harry
- but you have a total of 13 (1,2,5,5) in total - Excess Suslik
- First, you need to look for the terms only from 1, then from 1 and 2, then 1,2,3 and so on up to the number itself. Thus, the algorithm is divided into more understandable parts. You can understand how to get the next step from the previous one. - Dmitri Polyanin
- @Kasper Further need your work. - Dmitri Polyanin
1 answer
Let's look at a couple of examples:
Take the number 3. Divide it into a sum of units, the array looks like [1,1,1] .
Next, move the last unit to the first place (in this case, move = addition), it will turn out [2,1,0] , then we will do the same with the next unit to the right, we get [3, 0, 0] . So we got all the combinations of digits, which together give 3. We do not take into account the permutations [2, 1, 0] and [1, 2, 0]
Now let's do the same with the number 5, we get the following sequence of arrays.
[1,1,1,1,1] [2,1,1,1,0] [2,2,1,0,0] [3,2,0,0,0] [4,1,0,0,0] [5,0,0,0,0] The mechanism is the same, except that when we encounter not 1, we subtract 1 from this number and transfer it to the right (in fact, we do the same with 1, therefore we get zeros at the end of the array)
Another example with a bigger number, 8
[1,1,1,1,1,1,1,1] [2,1,1,1,1,1,1,0] [2,2,1,1,1,1,0,0] [2,2,2,1,1,0,0,0] [2,2,2,2,0,0,0,0] [3,2,2,1,0,0,0,0] [3,3,2,0,0,0,0,0] [4,3,1,0,0,0,0,0] [4,4,0,0,0,0,0,0] [5,3,0...] [6,2,0...] [7,1,0...] I think you caught the gist: at each iteration, we take the rightmost nonzero element, subtract one from it, and add it to the left element, which is also considered at iteration
Here is the js code:
const getNumberTerms = (n) => { if(!n) return [] const result = []; const terms = (new Array(n)).fill(1); result.push([...terms]); let willItterate = true; let last = n - 1; let current = 0; let minCurrent = 0 while(willItterate) { // перемещаем единицу terms[current] += 1; terms[last] -= 1; // добавляем массив в результат result.push([...terms]); // если элемент равен 9, то сдвигаем минимально значение курсора вправо if(terms[current] === 9) minCurrent++ // сдвигаем правый курсор вправо current++; // если самый левый элемент равен нулю, то сдвигаем левый курсор в лево if(terms[last] === 0) last--; // если правый курсор зашел на левый, значит, нужно сбросить его на минимальное значение if(current >= last) current = minCurrent; if(last === minCurrent) willItterate = false; } return result; }; // этот код для презентации const output = document.getElementById('output'); document.getElementById('input').onchange = function () { const result = getNumberTerms(+this.value); output.innerHTML = '' result.forEach(el => { output.innerHTML += `<br>${JSON.stringify(el)}` }) } <input id='input'> <pre id='output'> </pre> The complexity of the basic algorithm, it seems to be linear (did not check for sure) Then you can do anything with the answer, whatever. Don't like 0? You can remove them by passing a cycle (though quadratic complexity will be added). Need an array of a certain length? Add zeros at the end, or take the first N items
- and what is the name of this algorithm? - Excess Gophers
- one@ I don’t know exaggerated, I thought it up myself. Well, maybe he has a name - ThisMan
- An interesting algorithm, but not everyone finds solutions, scored 12, and there is no option [10,2] and [6,1,1,1,1,1,1] well, and others. - Dmitri Polyanin
- @DmitryPolyanin 10 cannot be by condition, but [6,1,1,1,1,1,1,1] is yes, my mistake is ThisMan
- @ThisMan yes I see about 10, I read it inattentively, we then added this information. - Dmitry Polyanin