用123456这六个数,用Java编写一个main函数打印出所有的排序。要求4不能在第三位,3与5不能相连

来源:百度知道 编辑:UC知道 时间:2024/05/12 17:54:39

不考虑效率。

for (int i = 123456; i <= 654321; i++) {
String tmp = "" + i;
if (tmp.matches("[1-6]{2}[12356][1-6]{3}") && tmp.replaceAll("(\\d)(?=\\d*\\1)|35|53", "").length() == 6 ) {
System.out.println(i);
}
}

考虑效率。

public static void f(String in, ArrayList al) {
if (al.size() == 1) {
String tmp = in ;
if (!(tmp.contains("35") || tmp.contains("53") || tmp.indexOf('4') == 2)) {
System.out.println(tmp);
}
} else {
ArrayList hsc = (ArrayList) al.clone();
if (in.length() != 0) {
hsc.remove(in.substring(in.length() - 1));
}
for (Object i : hsc) {
f(in + i, hsc);