Title Link: Sword Point offer
Topic Link: Arrange the array into the smallest number, for example {3, 32, 321} output: 321323
Problem-solving ideas: I now know the correct answer, there is no way to prove that the first meeting, in the meeting to think again.
Code:
#include <iostream>#include<cstdio>#include<string>#include<vector>#include<cstring>#include<iterator>#include<cmath>#include<algorithm>#include<stack>#include<deque>#include<map>#include<Set>#include<queue>#defineLson L, M, rt<<1#defineRson m+1, R, rt<<1|1#defineMem0 (a) memset (A,0,sizeof (a))#defineMem1 (a) memset (A,-1,sizeof (a))#defineSCA (x) scanf ("%d", &x)#defineDe printf ("=======\n")typedefLong Longll;using namespacestd;Const intG_maxnumberlength =Ten;Char* G_strcombine1 =New Char[2*g_maxnumberlength+1];Char* G_strcombine2 =New Char[2*g_maxnumberlength+2];intCompareConst void* StrNumber1,Const void*strNumber2) {strcpy (g_strcombine1,*(Const Char**) strNumber1); strcat (G_strcombine1,*(Const Char**) strNumber2); strcpy (G_strcombine2,*(Const Char**) strNumber2); strcat (G_strcombine2,*(Const Char**) strNumber1); returnstrcmp (G_strcombine1, g_strcombine2);}voidPrintminnumber (int*numbers,intlength) { if(numbers = = NULL | | length <=0)return; Char**strnumbers = (Char**)(New int[length]); for(inti =0; i < length; i++) {Strnumbers[i]= (Char*)(New Char[g_maxnumberlength+1] ); sprintf (Strnumbers[i],"%d", Numbers[i]); } qsort (strnumbers, Length,sizeof(Char*), compare); for(inti =0; i < length; i++) {printf ("%s", Strnumbers[i]); } printf ("\ n" ); for(inti =0; i < length; i++ ) { Delete[] strnumbers[i]; } Delete[] strnumbers;}intMain () {intdata[4] = {3,321, +}; Printminnumber (data,3); return 0;}
View Code
Thinking: I also think of a practice, and prove correctness. Our goal now is to re-order this array, for two numbers how can we judge who is superior, one of us to compare, if a number than a certain number dozen is obviously small that the number of excellent, but for a string is another string prefix it? We should be classified at this time to discuss, 32 to better than 3, 3 to better than 34, for what, we only need to consider the number of cases more than the case, because the small must have been lined up to the front, oops .... Not clearly expressed.
The sword refers to the offer surface question 33 to arrange the array into the smallest number