1 2 2 3 4 5生成4不在第3位 3 5不相邻的不重复的6位数
public class CreateNum {
// ******************************************************************************
private int[] numbers = new int[] { 1, 2, 2, 3, 4, 5 };
public int n;
private String lastResult = "";
private boolean validate(String s) {
if (s.compareTo(lastResult) <= 0)
return false;
if (s.charAt(2) == '4')
return false;
if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0)
return false;
return true;
}
public void list(String index, String result) {
for (int i = 0; i < numbers.length; i++) {
if (index.indexOf(i + 48) < 0) {
String s = result + String.valueOf(numbers[i]);
if (s.length() == numbers.length) {
if (validate(s)) {
System.out.println(s);
lastResult = s;
n++;
}
break;
}
list(index + String.valueOf(i), s);
}
}
}
// *****************************************************************************/
public static boolean validate2(String s) {
if (s.charAt(2) == '4') {
return false;
} else if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0) {
return false;
}
return true;
}
public void cmp2(String p, String ss) {
System.out.println("%%%%: " + p + " ####: " + ss);
if (ss.length() == 1) {
if (validate2(p + ss)) {
System.out.println(p + ss);
n++;
}
return;
}
for (int i = 0; i < ss.length(); i++) {
if (ss.indexOf(ss.charAt(i)) == i)
cmp2(p + ss.charAt(i), ss.substring(0, i)
+ ss.substring(i + 1, ss.length()));
}
}
// *****************************************************************************/
public static void main(String[] args) {
CreateNum t = new CreateNum();
t.list("", "");
System.out.println("总数:" + t.n);
System.out.println("***********************************************");
t.n = 0;
t.cmp2("", "122345");
System.out.println("总数:" + t.n);
}