For given number of blocks of digits (number of blocks is bigger than 2 and smaller than 1000), print the biggest possible number.
Here's a little example:
input:
5 // Number of blocks of digits
9 // first block
98 // second
90 // third
5 // fourth
9 // fifth
output:
9998905 // The biggest possible number
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++
Here's the algorithm:
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.
As i said before, this algorithm work fine, but i need the code, because i'm having problem with it.
Here's my work until now:
#include <iostream>
#include <string>>
using namespace std;
int main()
{
int nums, maxsl = 0;
cin >> nums;
string s[nums];
for(int i = 0; i<nums; i++)
{
cin >> s[i];
if(s[i].length() > maxsl)
{
maxsl = s[i].length();
}
}
for(int i = 0; i<nums; i++)
{
for(int j = 0; j<nums; j++)
{
for(int k = 0; k<=maxsl; k++)
{
if(k<=s[i].length() && k<= s[j].length())
{
if(s[i][k]>s[j][k])
{
string t = s[i];
s[i] = s[j];
s[j] = t;
}
}
else
{
if(s[i].length() > s[j].length())
{
string t = s[i];
s[i] = s[j];
s[j] = t;
}
}
}
}
}
for(int i = 0; i<nums; i++)
{
cout << s[i];
}
}
But with these code it only print them in ascending order, not the biggest posible number. Here's the output for previous example: 9890995