Arrange 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 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

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.