Note that there are some explanatory texts on larger screens.

plurals
  1. POBiggest possible number from given blocks of digits C++
    primarykey
    data
    text
    <p>For given number of blocks of digits (number of blocks is bigger than 2 and smaller than 1000), print the biggest possible number.</p> <p>Here's a little example:</p> <p>input:</p> <pre><code>5 // Number of blocks of digits 9 // first block 98 // second 90 // third 5 // fourth 9 // fifth </code></pre> <p>output:</p> <pre><code>9998905 // The biggest possible number </code></pre> <p>I work a little bit on this problem and i have found the algorithm and it seems like it's working for any combination, but i'm having problem with writing a code in C++</p> <p>Here's the algorithm:</p> <p>First i input them as a strings, because i can muc easier use specific digits. Then i'm comparing every number's first digit with the every number's first digit. And order them in ascending order. If the first digits are same, i'm checking the second digits and so on to the last digits. If in case the two number have different lengths and the smaller is substring to the other, i order the smaller in front of the bigger number.</p> <p>As i said before, this algorithm work fine, but i need the code, because i'm having problem with it.</p> <p>Here's my work until now:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt;&gt; using namespace std; int main() { int nums, maxsl = 0; cin &gt;&gt; nums; string s[nums]; for(int i = 0; i&lt;nums; i++) { cin &gt;&gt; s[i]; if(s[i].length() &gt; maxsl) { maxsl = s[i].length(); } } for(int i = 0; i&lt;nums; i++) { for(int j = 0; j&lt;nums; j++) { for(int k = 0; k&lt;=maxsl; k++) { if(k&lt;=s[i].length() &amp;&amp; k&lt;= s[j].length()) { if(s[i][k]&gt;s[j][k]) { string t = s[i]; s[i] = s[j]; s[j] = t; } } else { if(s[i].length() &gt; s[j].length()) { string t = s[i]; s[i] = s[j]; s[j] = t; } } } } } for(int i = 0; i&lt;nums; i++) { cout &lt;&lt; s[i]; } } </code></pre> <p>But with these code it only print them in ascending order, not the biggest posible number. Here's the output for previous example: 9890995</p>
    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.
 

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