Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is complete conjecture, since I'm not really good with math, but I think I'd go about it like this:</p> <pre><code> int potency = 0, base = 1; while(base &lt; NumberInQuestion) { base *= 2; ++potency; } </code></pre> <p>After the loop finishes, you'll know the highest potency which still fits into 'Number'.</p> <pre><code> Number -= base/2; //Removes the base you just calculated from the number. printf("2^%d", potency); </code></pre> <p>Rinse and repeat, until Number falls to 0, which should be at 2^0 at latest.</p> <p>For your use-case, the code may look somewhat like this:</p> <pre><code>for(i=0; i &lt; n; ++i) { int Number = niz[i]; while(Number &gt; 0) { int potency = 0, base = 1; do { //Executes at least once, making a number of '1' possible. base *= 2; ++potency; } while(base &lt; Number); Number -= base/2; //Reverts the last step you made, making the 'base' smaller than 'Number'. printf("2^%d", potency); } } </code></pre> <p>There's a possible alternative, which can give you a more complete picture of things and will save you iterations. For this we use a two-step process.</p> <pre><code>for(i=0; i &lt; n; ++i) { int Number = niz[i]; int potency = 0, base = 1; do { //Executes at least once, making a number of '1' possible. base *= 2; ++potency; } while(base &lt; Number); base /= 2; //Reverses the last iteration. //At this point, we know the maximum potency, which still fits into the number. //In regards of base 2, we know the Most Significant Bit. while(base &gt; 0) { Number -= base; //Removes the MSD (Most significant digit) printf("2^%d", potency); //Prints your '1'. while(base &gt; Number) { //Executes at least once. base /= 2; //Goes back one potency. (Ends at '0' latest.) --potency; //For each potency (except for first), it's a '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. This table or related slice is empty.
    1. 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