把数组中数字连起来组成一个最小的数字

import java.util.Arrays;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String result = “”;
while(scan.hasNext()){
int n = scan.nextInt();
int[] array = new int[n];
for(int i = 0; i < n; i++){
array[i] = scan.nextInt();
}
for(int i = 0; i< array.length; i++){
for(int j=0; j if(compare(array[i], array[j]) < 0){
int a = array[i];
array[i] = array[j];
array[j] = a;
}
}
}
for(int k=0; k result += array[k];
}
System.out.print(result) ;
}
}

public static int compare(int a,int b){
    String abStr = String.valueOf(a) + String.valueOf(b);
    String baStr = String.valueOf(b) + String.valueOf(a);
    if(Integer.valueOf(abStr) > Integer.valueOf(baStr))
      return 1;
    if(Integer.valueOf(abStr) < Integer.valueOf(baStr))
        return -1;
    return 0;
}

}

你可能感兴趣的:(java算法)