首页 > 代码库 > 含有n个元素的整型数组,将这个n个元素重新组合,求出最小的数,如{321,3,32},最小的数为321323
含有n个元素的整型数组,将这个n个元素重新组合,求出最小的数,如{321,3,32},最小的数为321323
public class GetMinNumber {
public static void main(String[] args) {
String[] arr = null;
System.out.println("输入一行待处理的整型数据,并以逗号隔开:");
Scanner input = new Scanner(System.in);
arr = input.nextLine().split(",");
sort(arr);
for(String s : arr){
System.out.print(s+"");
}
}
public static void sort(String[] arr) {
for(int i = 0; i < arr.length - 1; ++i){
for(int j = 0; j < arr.length-1; ++j){
int p = 0, q = 0;
String temp = "";
char[] c1 = arr[j].toCharArray();
char[] c2 = arr[j + 1].toCharArray();
while(p < c1.length && q < c2.length){
if(c1[p] > c2[q]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
break;
}
if(p == c1.length - 1 && q != c2.length - 1 && c1[p] > c2[q+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
break;
}
if(q == c2.length - 1 && p != c1.length - 1 && c1[p+1] > c2[q]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
break;
}
++p;
++q;
}
}
}
}
}
含有n个元素的整型数组,将这个n个元素重新组合,求出最小的数,如{321,3,32},最小的数为321323