Note that there are some explanatory texts on larger screens.

plurals
  1. POFind valid assignments of integers in arrays (permutations with given order)
    text
    copied!<p>I am having a general problem finding a good algorithm for generating each possible assignment for some integers in different arrays.</p> <p>Lets say I have n arrays and m numbers (I can have more arrays than numbers, more numbers than arrays or as much arrays as numbers). </p> <p>As an example I have the numbers 1,2,3 and three arrays:</p> <p>{ }, { }, { }</p> <p>Now I would like to find each of these solutions:</p> <pre><code>{1,2,3}, { }, { } { }, {1,2,3}, { } { }, { }, {1,2,3} {1,2}, {3}, { } {1,2}, { }, {3} { }, {1,2}, {3} {1}, {2,3}, { } {1}, { }, {2,3} { }, {1}, {2,3} {1}, {2}, {3} </code></pre> <p>So basically I would like to find each possible combination to assign the numbers to the different arrays with keeping the order. So as in the example the 1 always needs to come before the others and so on...</p> <p>I want to write an algorithm in C++/Qt to find all these valid combinations.</p> <p>Does anybody have an approach for me on how to handle this problem? How would I generate these permutations?</p> <p><strong>ADDITIONS</strong></p> <p>Unfortunately I didn't manage to change the great examples you gave for the problem I have now, since the numbers that I want to arrange in the arrays are stored in an array (or for me a QVector)</p> <p>Can anybody help me change the algorithm so that it gives me each possible valid combination of the numbers in the QVector to the QVector&lt; QVector > so that I can do further computations on each one?</p> <pre><code>QVector&lt;int&gt; line; // contains the numbers: like {7,3,6,2,1} QVector&lt; QVector&lt;int&gt; &gt; buckets; // empty buckets for the numbers { {}, {}, {} } QList&lt; QVector&lt; QVector&lt;int&gt; &gt; &gt; result; // List of all possible results </code></pre> <p>Would be really great if anyone could provide me with a simple implementation that works or tips on how to get it... I just couldn't change the code that was already provided so that it works...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload