題目說明:
一組數字,比如3,30,9,4,34等,求其能組成的最大數,應該是9434330
實現原理:
利用到了冒泡排序的原理,比較移換位置
代碼
private static boolean compare(int a, int b) {// 比較大小,前大爲true
String s1 = a + "" + b;
String s2 = b + "" + a;
int result = s1.compareTo(s2);
if (result < 0) {
return true;
}
return false;
}
private void fun() {
int[] a = new int[] { 3, 30, 9, 4, 34, 35 };
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (compare2(a[j], a[j + 1])) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}