题目
题解
参考
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int xmh = scanner.nextInt();
int n = scanner.nextInt();
int[] COs = new int[n];
for (int i = 0; i < n; i++) {
COs[i] = scanner.nextInt();
}
Arrays.sort(COs, (a, b) -> {
int diffA = Math.abs(xmh - a);
int diffB = Math.abs(xmh - b);
if (diffA == diffB) {
return Integer.compare(a, b);
}
return Integer.compare(diffA, diffB);
});
for (int i : COs) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
https://blog.csdn.net/weixin_52908342/article/details/135035516