import java.util.Scanner;
public class P1012拼数 {
// 局部最优解
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String[] ss = new String[n];
ss = sc.nextLine().split(" ");
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
String s1 = ss[i] + ss[j];
String s2 = ss[j] + ss[i];
if (s1.compareTo(s2) < 0) {
String temp = ss[i];
ss[i] = ss[j];
ss[j] = temp;
}
}
}
for (int i = 0; i < n; i++)
System.out.print(ss[i]);
sc.close();
}
}
[java]P1012拼数题解 全AC 代码简洁
最新推荐文章于 2025-12-19 15:39:34 发布
该Java程序读取用户输入的整数n和n个字符串,然后将相邻的字符串两两拼接,比较拼接后的字符串字典序,确保每个位置上的字符串都是局部最优解(即前面的字符串不小于后面的字符串)。最后输出排序后的字符串数组。
512

被折叠的 条评论
为什么被折叠?



