"Face question 33" rows the array into the smallest number

Source: Internet
Author: User

"Title description"

Enter an array of positive integers, combine all the numbers in the array into a number, and print the smallest of all the numbers that can be stitched together.

For example, the input array {3,32,321} prints out the smallest number 321323 that these three numbers can rank.

"The Test Center"

1. It is difficult to come up with new comparison rules to sort an array;

2. It is difficult to prove the array after sorting according to this rule, and connect the numbers in the immediate minimum number;

3. To solve the problem of large numbers, the sum of int may overflow, can be solved by string comparison;

"Solutions"

My code implementation is for reference only:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Collections;6 7 namespaceConsoleApplication18 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -             int[] arr =New int[] {2,1 }; - Printminnumber (arr); the         } -  -          Public Static voidPrintminnumber (int[] arr) -         { +             if(arr = =NULL|| Arr. Length <1) -                 return; +  A             string[] STRs =New string[arr. Length]; at  -              for(inti =0; I < arr. Length; i++) -             { -Strs[i] =Arr[i]. ToString (); -             } -  inArray.Sort (STRs,NewMycompare ()); -  to             foreach(stringStrinchSTRs) +             { - Console.Write (str); the             } *         } $     }Panax Notoginseng  -      Public classMycompare:icomparer the     { +          Public intCompare (ObjectObja,ObjectOBJB) A         { the             stringStra = (string) Obja; +             stringStrB = (string) OBJB; -  $             stringBegina = Strb.insert (0, stra); $             stringBEGINB = Stra.insert (0, StrB); -  -             returnBegina.compareto (BEGINB); the         } -     }Wuyi}

"Face question 33" rows 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.