/**
 * Java经典算法集——如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,
 * 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。 
 */
package com.test1;import java.util.Set;
import java.util.TreeSet;public class NumberSort {    public static Set<String> set = new TreeSet<String>();    public static void perm(char[] n, int beg, int end) {
        if (beg == end) {
            addNumber(String.valueOf(n));
        } else {
            for (int i = beg; i <= end; ++i) {
                swap(n, beg, i);
                perm(n, beg + 1, end);
                swap(n, beg, i);
            }
        }
    }    public static void swap(char[] n, int x, int y) {
        if (x == y || n[x] == n[y]) {
            return;
        }
        char temp = n[x];
        n[x] = n[y];
        n[y] = temp;
    }    public static void addNumber(String str) {
        if (str.charAt(2) == '4' || str.contains("35") || str.contains("53")) {
            return;
        }
        set.add(str);
    }    public static void main(String args[]) {
        char[] number = new char[] { '1', '2', '2', '3', '4', '5' };
        perm(number, 0, number.length - 1);
        System.out.println("共有:"+set.size());
        int cols = 10;
        System.out.println("分别是");
        for (String s : set) {
            System.out.print(s + " ");
            if (cols-- == 1) {
                System.out.println();
                cols = 10;
            }
        }
    }
}
//小弟实在看不懂perm方法和swap方法是干什么用的,请高手重点注释这两个方法,小弟感激不尽算法注释Java

解决方案 »

  1.   

       for (int i = beg; i <= end; ++i) {
                     swap(n, beg, i); 
                     perm(n, beg + 1, end); 
                     swap(n, beg, i); 
                 }
    从6个里面拿一个放到第1个位置,因为原来位置里也有,所以要交换。
    从剩下的5个里面拿一个放到第2个位置。
    接下来就是从剩下4个里面选一个,最后就剩一个了,直接拿过来输出就好了。
    输出后,再把原来的位置换回去。
    这里的输出结果直接在原数组里通过交换得到的,看上去不是那么直观。
      

  2.   

    public static void perm(char[] n, int beg, int end) {
    // 如果最后一个位置也排列好了就调用判断条件
            if (beg == end) {                 
                addNumber(String.valueOf(n));
            } else { //对前面的位置进行排列
             //从beg位置开始排直到end位置
                for (int i = beg; i <= end; ++i) {   
                    swap(n, beg, i);
                    perm(n, beg + 1, end);
                    swap(n, beg, i);
                }
            }
        }    public static void swap(char[] n, int x, int y) {
         //如果需要判断的位置一致就返回
            if (x == y || n[x] == n[y]) {
                return;
            }
            //不一致就对x和y的位置进行调换
            char temp = n[x];
            n[x] = n[y];
            n[y] = temp;
        }
      

  3.   

    我想问问撸主这里的代码是从哪里来的?Java经典算法集?是纸质书还是网上的电子文档。似乎看起来不错。能不能发我一份,我也研究研究....
      

  4.   

       //枚举出n数组中从beg到end的全部排列
        public static void perm(char[] n, int beg, int end) {
            //如果beg和end相等,对应排列只有一种,结束。
         if (beg == end) {
                addNumber(String.valueOf(n));
            } else {//如果beg和end不等。
                for (int i = beg; i <= end; ++i) {//枚举出beg位元素在各个位置上的排列情况
                    swap(n, beg, i);//beg位元素与i位元素互换
                    perm(n, beg + 1, end);//枚举出n数组中从beg+1到end的全部排列
                    swap(n, beg, i);//beg位元素与i位元素互换,恢复原样
                }
            }
        }    
       //在数组中交换下标是x和y的两个元素
        public static void swap(char[] n, int x, int y) {
         //下标相同或内容相同,不交换
         if (x == y || n[x] == n[y]) {        
                return;
            }
            //交换:
            char temp = n[x];
            n[x] = n[y];
            n[y] = temp;
        }