日期:2014-05-20  浏览次数:20701 次

跪求,裸求.....大神正解....一到十,十个数....
有{1,2,3,4,5,6,7,8,9,10}十个数字,要求相邻的两个数不能差的绝对值不能等于1,一共有多少种排列组合

------解决方案--------------------
呃 发错了 
Java code

public class Traversal {

    static long thesum = 0;

    public static void main(String[] args) {
        int buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

        perm(buf, 0, buf.length - 1);
        System.out.println("thesum is :" + thesum);
    }

    public static void perm(int[] buf, int start, int end) {
        if (start == end) {//当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可  
            for (int i = 1; i <= end; i++) {
                if (((buf[i] - buf[i - 1]) == 1) || ((buf[i] - buf[i - 1]) == -1)) { 
                    return;
                }
            }
            System.out.print("_" + thesum + " ");
            for (int i = 0; i <= end; i++) {
                System.out.print(buf[i]);
            }
            thesum++;
            System.out.println();
        } else {//多个字母全排列  
            for (int i = start; i <= end; i++) {
                int temp = buf[start];//交换数组第一个元素与后续的元素  
                buf[start] = buf[i];
                buf[i] = temp;

                perm(buf, start + 1, end);//后续元素递归全排列  

                temp = buf[start];//将交换后的数组还原  
                buf[start] = buf[i];
                buf[i] = temp;
            }
        }
    }
}