我们有一个3位数字数组,只对数组中间的数字排序我/p = 540984902O/p=500,944,982,使用一个数组
改变中间数我只尝试了交换选项。除了使用一个数组
是这样的吗?
int[] numbers = new int[]{540, 984, 902};// or however many 3-digit numbers
int[] mids = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
mids[i] = (numbers[i] / 10) % 10;
}
Arrays.sort(mids);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (numbers[i] / 100) * 100 + mids[i] * 10 + numbers[i] % 10;
System.out.println("a[" + i + "] " + numbers[i]);
}