Question: enter a positive integer array, splice all the numbers in the array into a number, and print the smallest number of clocks that can be combined. For example, if {3, 32,321} is input, the minimum number displayed is 321323.
Analysis: It is important to note that the number of components may overflow, so this is a big number problem. For the big number problem, we thought we could not directly form a number like this. We could use a string, that is, convert it into a string before comparison, strcmp ().
Another problem is that we can sort the characters without using a fully-arranged combination.
The Code is as follows:
// Redefine the comparison function object struct compare {bool operator () (const string & src1, const string & src2) {string S1 = src1 + src2; string S2 = src2 + src1; return S1 <S2; // in ascending order. If it is changed to S1> S2, it is in reverse order}. // function: sets the minimum number of arrays. // function parameters: parray is an array, and num is the number of array elements // return value: no void comarraymin (int * parray, int num) {int I; string * pstrarray = new string [num]; for (I = 0; I <num; I ++) // convert a number to a string {stringstream; stream <parray [I]; stream> pstrarray [I];} Sort (pstrarray, pstrarray + num, compare (); // sort string Arrays for (I = 0; I <num; I ++) // print the string array cout <pstrarray [I]; cout <Endl; Delete [] pstrarray ;}
Reference: http://blog.csdn.net/wuzhekai1985/article/details/6704902