- package testWork;
-
- public class Test1
- {
- private int[] numbers = new int[] {1, 2, 3, 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')//"4"不能在第三位
- {
- return false;
- }
- if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0)//"3"与"5"不能相连
- {
- 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 void main(String[] args)
- {
- Test1 t = new Test1();
- t.list("", "");
- System.out.println("总数:" + t.n);
- }
- }
复制代码 |