import java.util.Scanner; public class Pailie { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String str = n + ""; String s[] = str.split(""); int a[] = new int[s.length]; for (int i = 0; i < s.length; i++) { a[i] = Integer.parseInt(s[i]); } a = sort(a); int m = s.length; int b[] = new int[s.length]; f(a,m,b,""); } private static void f(int[] a, int m, int[] b, String se) { if(m == 0) { System.out.println(se); } for (int j = 0; j < a.length; j++) { if(b[j] == 1) continue; b[j] = 1; f(a,m-1,b,se+a[j]); if(j<a.length-1 &&a[j]==a[j+1]) { b[j] = 0; j = j + h(a,j+1); continue; } b[j] = 0; continue; } } private static int h(int[] a, int j) { int w = 1; for (int i = j; i < a.length-1; i++) { if(a[j] == a[i+1]) { w++; } } return w; } private static int[] sort(int[] a) { for (int i = 0; i < a.length-1; i++) { for (int j = 0; j < a.length-1; j++) { if(a[j]>a[j+1]) { int t = a[j]; a[j] = a[j+1]; a[j+1] = t; } } } return a; } }
sort():升序排列
h():剔除重复代码