1 package karl.test;
2
3 import java.util.Iterator;
4 import java.util.Set;
5 import java.util.TreeSet;
6
7 public class Test {
8 private static Set<String> set = new TreeSet<String>();
9
10 public static String sort(String prex, String str) {
11 boolean flag = true;
12 int len = str.length();
13 if (str.length() == 1) {
14 set.add(prex + str);
15 flag = false;
16 return prex + str;
17 }
18
19 for (int i = 0; i < len; i++) {
20 String reststr = str.substring(0, i) + str.substring(i + 1, len);
21 prex += str.charAt(i);
22 sort(prex, reststr);
23 if (flag) {
24 prex = prex.substring(0, prex.length() - 1);
25 flag = true;
26 }
27 }
28 return null;
29 }
30
31 public static void main(String[] args) {
32 String str = "122345";
33 sort("", str);
34 Iterator<String> it = set.iterator();
35 while (it.hasNext()) {
36 String next = it.next();
37 if (next.contains("35") || next.contains("53")
38 || (next.charAt(3) == '4')) {
49
40 } else {
41 System.out.println(next);
42 }
43 }
44 }
45 }
2
3 import java.util.Iterator;
4 import java.util.Set;
5 import java.util.TreeSet;
6
7 public class Test {
8 private static Set<String> set = new TreeSet<String>();
9
10 public static String sort(String prex, String str) {
11 boolean flag = true;
12 int len = str.length();
13 if (str.length() == 1) {
14 set.add(prex + str);
15 flag = false;
16 return prex + str;
17 }
18
19 for (int i = 0; i < len; i++) {
20 String reststr = str.substring(0, i) + str.substring(i + 1, len);
21 prex += str.charAt(i);
22 sort(prex, reststr);
23 if (flag) {
24 prex = prex.substring(0, prex.length() - 1);
25 flag = true;
26 }
27 }
28 return null;
29 }
30
31 public static void main(String[] args) {
32 String str = "122345";
33 sort("", str);
34 Iterator<String> it = set.iterator();
35 while (it.hasNext()) {
36 String next = it.next();
37 if (next.contains("35") || next.contains("53")
38 || (next.charAt(3) == '4')) {
49
40 } else {
41 System.out.println(next);
42 }
43 }
44 }
45 }