To rank the array into the smallest number

Source: Internet
Author: User

Title: Enter an array of positive integers, combine all the numbers in the array into a number, and the print can stitch up the smallest of all the numbers. For example, the input array {3,32,321} prints out the smallest number 321323 that these three numbers can be ranked.

Analysis: The most straightforward approach is to first find all the numbers in this array of all the permutations, and then put each arrangement together, to find the smallest one to spell the number. It's too time-complicated to do this.

in fact, the problem is that we have to find a collation in which the array is sorted by this rule to be the smallest number. This rule should be two digits m,n, if mn<nm defines m less than N, whereas NM<MN defines n less than M. If MN=NM defines n equals M. At the same time consider the large number of problems, it is possible to splice up after the expression range of int type. So the numbers are converted to strings. Because MN and NM have the same number of bits, you only need to compare them by string size.

The implementation is as follows: Its time complexity is O (NLOGN).

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 1=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;} Int compare (Const 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);   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;STRCMP (g_strcombine1,g_strcombine2);}


This article from "Fairy Road thousands of dust Dream" blog, please be sure to keep this source http://secondscript.blog.51cto.com/9370042/1586553

To rank the array into the smallest number

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.