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.

Source: Internet
Author: User

/**
* @Version 1.0.0
* enter an array of positive integers to stitch together all the numbers in the array into a number,
* The smallest one that prints all the numbers that can be spliced out. For example, enter{3,32,321,4}, the minimum number to print is
* 321323
*
* is actually a sort of, but the comparison of the size of the way and the value is different
* Here's a bubble sort of way
*/
Public classPro33 {

@MyAnswer
Public static voidF(intArray[]) {
for(intI=0;I<array.length-1;i++) {
intbig=0;
for(intj=0;J<array.length-I.;J + +) {
if(Compare(String.valueOf(Array[j]),String.valueOf(Array[big])) >0){
Big=j;
}
}
if(Big!=array.length-i-1){
intTemp=array[big];
Array[big]=array[array.length-i-1];
Array[array.length-i-1]=temp;
}
}
for(intA:array) {
System. out. Print (A +" ");
}
}
@MyAnswer
public static intCompare(String A, String b) {
String left=a+b;
String Right=b+a;
for(intI=0;I<left.length ()-1;i++) {
if(Left.charat (i) >right.charat (i)) {
return1;
}else if(Left.charat (i) <right.charat (i)) {
return-1;
}
}
return0;
}

Public static voidMain(String args []) {
intarray[]=/*{23,345, 121,42,1}*/{3, +,321,4};
Pro33.F(array);
}
}

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.

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.