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 of all the numbers that can be spliced. For example, enter an array, {32,321 }, print the minimum number 32132 that can be arranged with the three numbers.

Sort the original integer array by comparing the size of the combined string. For example, 32 and 321 compare the result with "32321"> "32132", so 32 is considered to be greater than 321. Solve this problem by using the Bubble sorting method.

Int compare2int (int A, int B) {// compare the size of char S1 [10], S2 [10], tmp1 [10] of string a + B and string B + A; sprintf (S1, "% d", a); sprintf (tmp1, "% d", a); sprintf (S2, "% d", B); strcat (tmp1, s2); strcat (S2, S1); int result = strcmp (tmp1, S2); // compare the return result of AB and BA;} void bubblesort (int A [], int Len) {int TMP; For (INT I = 0; I <Len; I ++) {for (Int J = 1; j <len-I; j ++) {If (compare2int (A [J-1], a [J]) = 1) {// If a [J-1] a [J] is greater than a [J] a [J-1], it is considered that a [J-1] is greater than a [J] to exchange them TMP = A [J]; A [J] = A [J-1]; A [J-1] = TMP ;}}} for (int K = 0; k <Len; k ++) {printf ("% d", a [k]) ;}printf ("\ n ");} int main () {int test [] = {3, 32, 321}; bubblesort (test, 3); Return 0 ;}


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