- package com.visionsky;
- public class test {
- private char[] numbers = new char[] { 'a', 'b', 'c','d','e'};
- public int n;
- private String lastResult = "";
- private boolean validate(String s) {
- if (s.compareTo(lastResult) <= 0)
- return false;
- if (s.charAt(2) == 'b')
- return false;
- if (s.indexOf("ce") >= 0 || s.indexOf("ec") >= 0)
- return false;
- return true;
- }
- /*
- * index 用于判断当前result中存有字符串的序号
- * result 当前运行时字符串的结果
- */
- public void list(String index, String result) {
- for (int i = 0; i < numbers.length; i++) {
- if (index.indexOf(i + 48) < 0) {//i+48 表示当前i的ascii码,即对i做char型类型转换,判断当前字符是否存在于result中
- 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) {
- test t = new test();
- t.list("", "");
- System.out.println("总数:" + t.n);
- }
- }