package myPro;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] count = new int[10];
String a = sc.next();
String b = sc.next();
int[] c = new int[20];
for (int i = 0; i < a.length(); i++) {
++count[(int) a.charAt(i) - 48];
}
if (a.length() < b.length()) {
myMax(count);
} else {
dfs(a.length(), 0, c, count, b);
for (int i = 0; i < a.length(); i++) {
System.out.print(c[i]);
}
}
}
public static void myMax(int[] count) {
for (int i = 9; i >= 1; i--) {
while (count[i]-- > 0) {
System.out.print(i);
}
}
}
public static boolean dfs(int m, int cur, int[] c, int[] count, String b) {
if (cur == m)
return true;
int i = (int) b.charAt(cur) - 48;
if (count[i] > 0) {
c[cur] = i;
--count[i];
if (dfs(m, cur + 1, c, count, b))
return true;
++count[i];
}
for (i = i - 1; i >= 0; i--) {
if (count[i] > 0) {
c[cur]=i;
--count[i];
int k =cur+1;
for (int j = 9; j >= 1; j--) {
while (count[j]-- > 0) {
c[k++]=j;
}
}
return true;
}
}
return false;
}
}
提交上去提示runtime error,请大神指正!