I just took online test for C++ programmer position, it was really interesting; But for porting programmer like me -which implementing anything is mere only on memory :p – this question is really hard for me.

Here is the question :

Write a function `arrange` that can take any non-negative integer as a argument and return it with it’s digits in descending order. Descending order means that you take the highest digit and place the next highest digit immediately after it. e.g.

arrange(145263)

=> 654321

arrange(1254859723)

=> 9875543221

Sorting is very easy, I have a lot of premade function to handle it. Processing the number was made me numb for some minutes, I took 20 minutes to figure out how to do it – maan I’m really stupid, it’s really easy! – .

int arrange(int y)
{
int b = y;
int value[100];
int size=0;
while(b!=0){
int tenth = pow(10,size +1);
value[size] = (b % tenth)/ (tenth/10);
b -= value[size]*(tenth/10);
size++;
}
*selSort(value,size);*
for(int in=0;in<size;in++){
cout<<value[in];
}
}

That’s the function I created to process numbers to array of integer, so I could sort it. Well I wrote it in hurry because I spent too much time on numbing. Look, that function is not good, there are some technique to simplify it.

### Like this:

Like Loading...

*Related*

Bisa di jelasin gimana maksud alurnya?

Untuk mendapatkan bilangan perdigitnya dilakukan pembagian 10^n di mana n adalah urutan bilangan.