Files
Java/作业/刘航宇-day43/第一题/P2895.java
T
2025-12-28 21:05:06 +08:00

51 lines
1.7 KiB
Java

import java.util.*;
import java.util.regex.*;
public class P2895 {
static String normalize(String s) {
boolean neg = s.startsWith("-");
String t = neg ? s.substring(1) : s;
int i = 0;
while (i < t.length() - 1 && t.charAt(i) == '0') i++;
t = t.substring(i);
if (t.equals("0")) neg = false;
return neg ? ("-" + t) : t;
}
static int cmpNum(String a, String b) {
boolean na = a.startsWith("-");
boolean nb = b.startsWith("-");
if (na != nb) return na ? -1 : 1;
String aa = na ? a.substring(1) : a;
String bb = nb ? b.substring(1) : b;
if (!na) {
if (aa.length() != bb.length()) return aa.length() < bb.length() ? -1 : 1;
int c = aa.compareTo(bb);
return c < 0 ? -1 : (c > 0 ? 1 : 0);
} else {
if (aa.length() != bb.length()) return aa.length() > bb.length() ? -1 : 1;
int c = aa.compareTo(bb);
return c > 0 ? -1 : (c < 0 ? 1 : 0);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
StringBuilder input = new StringBuilder();
while (in.hasNextLine()) {
if (input.length() > 0) input.append('\n');
input.append(in.nextLine());
}
Matcher m = Pattern.compile("-?\\d+").matcher(input.toString());
List<String> nums = new ArrayList<>();
while (m.find()) nums.add(normalize(m.group()));
nums.sort(P2895::cmpNum);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nums.size(); i++) {
if (i > 0) sb.append(',');
sb.append(nums.get(i));
}
System.out.println(sb.toString());
in.close();
}
}