按照要求写排序即可
import java.math.BigInteger;
import java.util.*;
public class Main {
static final int N = 100010, M = 1010;
static P[] p = new P[M];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
p[i] = new P(sc.next());
}
Arrays.sort(p, 0, n, (o1, o2) -> {
if (o1.len != o2.len) return Math.toIntExact(o1.len - o2.len);
BigInteger b1 = new BigInteger(o1.num);
BigInteger b2 = new BigInteger(o2.num);
return b1.compareTo(b2);
});
for (int i = 0; i < n; i++) {
System.out.println(p[i].num);
}
sc.close();
}
}
class P {
String num;
long len;
public P (String num) {
this.num = num;
int t = num.length();
int[] arr = new int[t], tmp = new int[t];
for (int i = 0; i < t; i++) {
arr[i] = this.get(i) - '0';
}
this.len = merge_sort(arr, 0, t - 1, tmp);
}
public char get(int i) {
return num.charAt(i);
}
public long merge_sort(int[] a, int l, int r, int[] t) {
if (l >= r) return 0;
int mid = l + r >> 1;
long res = merge_sort(a, l, mid, t) + merge_sort(a, mid + 1, r, t);
int i = l, j = mid + 1, k = 0;
while (i <= mid && j <= r) {
if (a[i] <= a[j]) t[k++] = a[i++];
else {
t[k++] = a[j++];
res += mid - i + 1;
}
}
while (i <= mid) t[k++] = a[i++];
while (j <= r) t[k++] = a[j++];
for (i = l, j = 0; i <= r; i++, j++)
a[i] = t[j];
return res;
}
}