Given an array containing only positive numbers, a method is provided to concatenate the numbers in the array. The maximum number is obtained. For example, after splicing [4, 94, 9, 14, 1], the maximum number obtained is 9944141.
Analysis
We can compare two numbers as a whole. Then, the result is obtained after sorting. Example: 5, 54, 56
Compared to 5 and 54, it is actually larger than 545 and 554.
Compared to 5 and 56, it is actually larger than 556 and 565.
Compared to 54 and 56, it is actually larger than 5456 and 5654.
Then let's make a change to the fast-paced program. When we compare two numbers A and B, we can compare the sizes of the numbers AB and Ba. However, the comparison has changed, and the rest is the same as that of the fast sort. To prevent overflow, we put two numbers in a string for comparison. The Code is as follows:
Char num1 [30], num2 [30]; bool CMP (INT value1, int value2) {char a [15], B [15]; sprintf (, "% d", value1); sprintf (B, "% d", value2); strcpy (num1, ); // concatenate two numbers into an array to compare strcat (num1, B); strcpy (num2, B); strcat (num2, a); int res = strcmp (num1, num2); If (RES> = 0) return true; return false;} string makebignum (vector <int> & Data) {int length = data. size (), I; If (length <= 0) return NULL; sort (data. begin (), Data. end (), CMP); // sort string res = ""; for (I = 0; I <length; I ++) {stringstream SS; SS <data [I]; Res + = ss. STR ();} return res ;}