The sword refers to offer (33) which sorts the array into the smallest number.

Source: Internet
Author: User

Question: enter a positive integer array, splice all the numbers in the array into a number, and print the smallest of all the numbers that can be spliced.

For example, if the input array is {3, 32,321}, the minimum number 321323 that can be arranged for the three numbers will be printed.

The two numbers M and N can be spliced into numbers Mn AND nm. If Mn <nm, the Mn is printed, that is, M is placed before n. We define that M is less than N at this time, that is, the function pointer that customizes the qsort sorting.

The number to be spliced in this question may be out of the expression range and must be addressed in large numbers.

Int compare (const void * strnumber1, const void * strnumber2); // int integers can be expressed in decimal notation with a maximum of 10 const int g_maxnumberlength = 10; char * g_strcombine1 = new char [g_maxnumberlength * 2 + 1]; char * g_strcombine2 = new char [g_maxnumberlength * 2 + 1]; void printminnumber (int * numbers, int length) {If (numbers = NULL | length <= 0) return; char ** strnumbers = (char **) (new int [length]); for (INT I = 0; I <length; ++ I) {strnumbers [I] = new char [g_maxnumberlength + 1]; sprintf (strnumbers [I], "% d", numbers [I]);} qsort (strnumbers, length, sizeof (char *),Compare); For (INT I = 0; I <length; ++ I) printf ("% s", strnumbers [I]); printf ("\ n "); for (INT I = 0; I <length; ++ I) Delete [] strnumbers [I]; Delete [] strnumbers ;} // If [strnumber1] [strnumber2]> [strnumber2] [strnumber1], the return value is greater than 0 // If [strnumber1] [strnumber2] = [strnumber2] [strnumber1], return value is equal to 0 // If [strnumber1] [strnumber2] <[strnumber2] [strnumber1], return value is less than 0int compare (const void * strnumber1, const void * strnumber2) {// [strnumber1] [strnumber2] strcpy (g_strcombine1, * (const char **) strnumber1); strcat (g_strcombine1, * (const char **) strnumber2 ); // [strnumber2] [strnumber1] strcpy (g_strcombine2, * (const char **) strnumber2); strcat (g_strcombine2, * (const char **) strnumber1 ); return strcmp (g_strcombine1, g_strcombine2 );}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.