Tag: Arrange the array into the smallest (large) number
This is a classic face test, the first time you see this problem we may think of the whole arrangement, and then put together each arrangement, and finally we find out the minimum value can be, below we say a faster algorithm.
In fact, we can find a sort of collation, the array according to the collation can be ranked as a minimum number. To determine the collation, we'll compare two numbers, such as M and N, and we need a rule to determine which of M and N should be in front, and not just compare the size of these two numbers.
One of the potential problems we need to consider about this problem is that if both M and N are within the range of int's expression, what happens after they are spliced? What about exceeding the range of int? These are the issues that we need to consider, which is a large number of hidden problems. We all know that we can solve the problem of large numbers by converting the numbers into strings, so that we can load the number of digits, and then compare the size of the numbers they are stitching into by comparing them directly to the size of the string. Let's look at the specific code below.
#include <stdio.h> #include <stdlib.h> #include <string.h>const int g_ maxnumberlength = 10; //the maximum number of digits that can be represented by the shaping Char *g_strcombine1 = new char[g_ maxnumberlength*2 + 1]; //is used to store strings that exceed the number of shaper digits char *g_strcombine2 = new Char[g_maxnumberlength*2 + 1];int compare (const void* strnumber1, const VOID*&NBSP;STRNUMBER2)//Custom comparison function, compare two string size { strcpy (g_strcombine1, * (const char**) strNumber1); strcat (g_strcombine1, * (const char**) strNumber2) strcpy (g_strcombine2, * (const char**) strNumber2) strcat (g_strcombine2, * (const char**) strNumber1); return strcmp (g_strcombine1, g_strcombine2) >0;//Note here is greater than 0 by default in ascending order, less than 0 descending}void printfminnumber (int* Number, int length) { if (number == null | | length < 0) { return; } char** strnumbers = (char**) (New int[length]); //defines a two-dimensional array int i = 0; for (; i < length; ++i) { strNumbers[i] = Each element in the new char[g_maxnumberlength + 1]; // array is a char-type pointer sprintf (strNumbers [i], "%d", number[i]); } qsort (Strnumbers, length, sizeof (char*), compare); i = 0; for (; i < length; i++) { printf ("%s", strnumbers[i]); } printf ("\ n"); i = 0; for (; i < length; ++i) { delete[] strnumbers[i]; } delete[] strnumbers;} Int main () { int arr[3] = { 12, 3, 4};//The contents of the array here itself can be int Len = sizeof (arr) / sizeof (arr[0]); printfminnumber (Arr, len); return 0;}
If it is the contents of the above array, I run the result under vs2013 as follows:
650) this.width=650; "title=" ~iuw35ja$h6tj@tvqlw%4nw.png "src=" http://s1.51cto.com/wyfs02/M00/80/F0/ Wkiom1dfgenj-nljaaaewaokl5a439.png "alt=" Wkiom1dfgenj-nljaaaewaokl5a439.png "/>
We can see that, as we expected, the array content is the smallest integer we need.
To rank the array into the smallest (large) number