Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This will do it:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; vector&lt;unsigned int&gt; foo(unsigned int n) { vector&lt;unsigned int&gt; ans; // One of the combinations is not to change anything, i.e. number itself ans.push_back(n); for(unsigned int i = 0; i &lt; 32; i++) { // Combinations with only one bit changed ans.push_back(n ^ (1 &lt;&lt; i)); for(unsigned int j = 0; j &lt; i; j++) { // Combinations with two bits changed ans.push_back(n ^ (1 &lt;&lt; i) ^ (1 &lt;&lt; j)); } } return ans; } int main() { vector&lt;unsigned int&gt; v = foo(0); for(unsigned int i = 0; i &lt; v.size(); i++) { cout &lt;&lt; v[i] &lt;&lt; endl; } return 0; } </code></pre> <p>P.S. Here is modified code according to description change:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; /* start denotes bit number from which we should start loop, i.e. we can't modify any bits before that bit to avoid duplicates (we are modifying bits with order from lowest to highest, so if we have modified some bit, next bit to modify should be a higher one. */ void foo(vector&lt;unsigned int&gt;&amp; ans, unsigned int number, int n, unsigned int start) { // As we reached to current number someway then it is one of the combinations ans.push_back(number); if(n &lt; 1) { // No more changes allowed, go back return; } // Try change one bit for(unsigned int i = start; i &lt; 32; i++) { foo(ans, number ^ (1 &lt;&lt; i), n - 1, i + 1); } } int main() { vector&lt;unsigned int&gt; v; unsigned int startingNumber = 0; int changesAllowed = 2; foo(v, startingNumber, changesAllowed, 0); for(unsigned int i = 0; i &lt; v.size(); i++) { cout &lt;&lt; v[i] &lt;&lt; endl; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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