题是这么样,有1,2,2,3,4,5这么,六个数字,要求把这六个数所有的排列都打印出来。4不能在第三位上,3和5不能相连,如123425,523142这个算法怎么实现谢谢了

解决方案 »

  1.   

    这问题见过了。这里是我的答案:
    http://yiding-he.javaeye.com/blog/56950
      

  2.   

    前些天刚好有看到这个的算法,调了个程序,楼主仅供参考
    package sort;import java.util.ArrayList;/**
     * 对 {1, 2, 2, 3, 4, 5} 进行无重复的全排列,要求 4 不在第三位,且 3,5 不相连。
     */
    public class AllSort
    { static ArrayList<int[]> results = new ArrayList<int[]>(); public static void main(String[] args)
    {
    int[] ints = { 1, 2, 2, 3, 4, 5 };
    roll(ints, 0);
    for (int[] result : results)
    {
    printArr(result);
    }
    System.out.println("结果数量:" + results.size());
    } /**
     * 递归构造全排列假设要排列的数组已经从小到大排好序。
     * @param ints   要排列的数组
     * @param pointer    进行排列的起始位置
     */
    private static void roll(int[] ints, int pointer)
    {
    if (ints.length < 2)
    {
    return;
    } if (pointer == ints.length - 1)
    {
    saveResult(ints);
    return;
    } int[] cloned;
    for (int i = pointer; i < ints.length; i++)
    {
    cloned = ints.clone();
    int t = cloned[i];
    cloned[i] = cloned[pointer];
    cloned[pointer] = t; roll(cloned, pointer + 1);
    }
    } /**
     * 判断数组是否与现有结果重复且满足条件,如果是则存入结果。
     * @param ints  要保存的数组
     */
    private static void saveResult(int[] ints)
    {
    boolean exists = false; for (int[] result : results)
    {
    if (same(result, ints))
    {
    exists = true;
    break;
    }
    } if (!exists && available(ints))
    {
    results.add(ints);
    }
    } /**
     * 比较两个数组是否完全相同
     * @param a       一个数组
     * @param b       另一个数组
     * @return 如果数组中每个元素都相同,则返回 true。
     */
    private static boolean same(int[] a, int[] b)
    {
    boolean same = true;
    for (int i = 0; i < a.length; i++)
    {
    if (a[i] != b[i])
    {
    same = false;
    break;
    }
    }
    return same;
    } /**
     * 输出数组
     * @param ints          要输出的数组
     */
    private static void printArr(int[] ints)
    {
    for (int n : ints)
    {
    System.out.print(n + " ");
    }
    System.out.println();
    } /**
     * 检查数组是否符合要求
     * @param ints      要检查的数组
     * @return 如果 4 在第三位,或者 3 和 5 相连,则返回 false。
     */
    private static boolean available(int[] ints)
    {
    return ints[2] != 4
    && (Math.abs(indexOf(ints, 3) - indexOf(ints, 5)) > 1);
    } /**
     * 查找指定数字在数组中的位置
     * @param ints         指定数组
     * @param n         指定数字
     * @return 指定数字在数组中的位置。如果找不到,则返回 -1。
     */
    private static int indexOf(int[] ints, int n)
    {
    for (int i = 0; i < ints.length; i++)
    {
    if (ints[i] == n)
    {
    return i;
    }
    }
    return -1;
    }
    }运行结果:
    1 2 2 3 4 5 
    1 2 2 5 4 3 
    1 2 3 2 4 5 
    1 2 3 2 5 4 
    1 2 3 4 2 5 
    1 2 3 4 5 2 
    1 2 5 4 3 2 
    1 2 5 4 2 3 
    1 2 5 2 4 3 
    1 2 5 2 3 4 
    1 3 2 2 4 5 
    1 3 2 2 5 4 
    1 3 2 4 2 5 
    1 3 2 4 5 2 
    1 3 2 5 4 2 
    1 3 2 5 2 4 
    1 4 2 3 2 5 
    1 4 2 5 2 3 
    1 4 3 2 2 5 
    1 4 3 2 5 2 
    1 4 5 2 3 2 
    1 4 5 2 2 3 
    1 5 2 3 4 2 
    1 5 2 3 2 4 
    1 5 2 4 3 2 
    1 5 2 4 2 3 
    1 5 2 2 4 3 
    1 5 2 2 3 4 
    2 1 2 3 4 5 
    2 1 2 5 4 3 
    2 1 3 2 4 5 
    2 1 3 2 5 4 
    2 1 3 4 2 5 
    2 1 3 4 5 2 
    2 1 5 4 3 2 
    2 1 5 4 2 3 
    2 1 5 2 4 3 
    2 1 5 2 3 4 
    2 2 1 3 4 5 
    2 2 1 5 4 3 
    2 2 3 1 4 5 
    2 2 3 1 5 4 
    2 2 3 4 1 5 
    2 2 3 4 5 1 
    2 2 5 4 3 1 
    2 2 5 4 1 3 
    2 2 5 1 4 3 
    2 2 5 1 3 4 
    2 3 2 1 4 5 
    2 3 2 1 5 4 
    2 3 2 4 1 5 
    2 3 2 4 5 1 
    2 3 2 5 4 1 
    2 3 2 5 1 4 
    2 3 1 2 4 5 
    2 3 1 2 5 4 
    2 3 1 4 2 5 
    2 3 1 4 5 2 
    2 3 1 5 4 2 
    2 3 1 5 2 4 
    2 4 2 3 1 5 
    2 4 2 5 1 3 
    2 4 3 2 1 5 
    2 4 3 2 5 1 
    2 4 3 1 2 5 
    2 4 3 1 5 2 
    2 4 1 3 2 5 
    2 4 1 5 2 3 
    2 4 5 1 3 2 
    2 4 5 1 2 3 
    2 4 5 2 1 3 
    2 4 5 2 3 1 
    2 5 2 3 4 1 
    2 5 2 3 1 4 
    2 5 2 4 3 1 
    2 5 2 4 1 3 
    2 5 2 1 4 3 
    2 5 2 1 3 4 
    2 5 1 3 4 2 
    2 5 1 3 2 4 
    2 5 1 4 3 2 
    2 5 1 4 2 3 
    2 5 1 2 4 3 
    2 5 1 2 3 4 
    3 2 2 1 4 5 
    3 2 2 1 5 4 
    3 2 2 4 1 5 
    3 2 2 4 5 1 
    3 2 2 5 4 1 
    3 2 2 5 1 4 
    3 2 1 2 4 5 
    3 2 1 2 5 4 
    3 2 1 4 2 5 
    3 2 1 4 5 2 
    3 2 1 5 4 2 
    3 2 1 5 2 4 
    3 2 5 1 4 2 
    3 2 5 1 2 4 
    3 2 5 4 1 2 
    3 2 5 4 2 1 
    3 2 5 2 4 1 
    3 2 5 2 1 4 
    3 1 2 2 4 5 
    3 1 2 2 5 4 
    3 1 2 4 2 5 
    3 1 2 4 5 2 
    3 1 2 5 4 2 
    3 1 2 5 2 4 
    3 1 5 2 4 2 
    3 1 5 2 2 4 
    3 1 5 4 2 2 
    3 4 2 1 2 5 
    3 4 2 1 5 2 
    3 4 2 2 1 5 
    3 4 2 2 5 1 
    3 4 2 5 2 1 
    3 4 2 5 1 2 
    3 4 1 2 2 5 
    3 4 1 2 5 2 
    3 4 1 5 2 2 
    3 4 5 1 2 2 
    3 4 5 2 1 2 
    3 4 5 2 2 1 
    4 2 2 3 1 5 
    4 2 2 5 1 3 
    4 2 3 2 1 5 
    4 2 3 2 5 1 
    4 2 3 1 2 5 
    4 2 3 1 5 2 
    4 2 1 3 2 5 
    4 2 1 5 2 3 
    4 2 5 1 3 2 
    4 2 5 1 2 3 
    4 2 5 2 1 3 
    4 2 5 2 3 1 
    4 3 2 2 1 5 
    4 3 2 2 5 1 
    4 3 2 1 2 5 
    4 3 2 1 5 2 
    4 3 2 5 1 2 
    4 3 2 5 2 1 
    4 3 1 2 2 5 
    4 3 1 2 5 2 
    4 3 1 5 2 2 
    4 1 2 3 2 5 
    4 1 2 5 2 3 
    4 1 3 2 2 5 
    4 1 3 2 5 2 
    4 1 5 2 3 2 
    4 1 5 2 2 3 
    4 5 2 3 1 2 
    4 5 2 3 2 1 
    4 5 2 1 3 2 
    4 5 2 1 2 3 
    4 5 2 2 1 3 
    4 5 2 2 3 1 
    4 5 1 3 2 2 
    4 5 1 2 3 2 
    4 5 1 2 2 3 
    5 2 2 3 4 1 
    5 2 2 3 1 4 
    5 2 2 4 3 1 
    5 2 2 4 1 3 
    5 2 2 1 4 3 
    5 2 2 1 3 4 
    5 2 3 2 4 1 
    5 2 3 2 1 4 
    5 2 3 4 2 1 
    5 2 3 4 1 2 
    5 2 3 1 4 2 
    5 2 3 1 2 4 
    5 2 1 3 4 2 
    5 2 1 3 2 4 
    5 2 1 4 3 2 
    5 2 1 4 2 3 
    5 2 1 2 4 3 
    5 2 1 2 3 4 
    5 4 2 3 2 1 
    5 4 2 3 1 2 
    5 4 2 2 3 1 
    5 4 2 2 1 3 
    5 4 2 1 2 3 
    5 4 2 1 3 2 
    5 4 3 2 2 1 
    5 4 3 2 1 2 
    5 4 3 1 2 2 
    5 4 1 3 2 2 
    5 4 1 2 3 2 
    5 4 1 2 2 3 
    5 1 2 3 4 2 
    5 1 2 3 2 4 
    5 1 2 4 3 2 
    5 1 2 4 2 3 
    5 1 2 2 4 3 
    5 1 2 2 3 4 
    5 1 3 2 4 2 
    5 1 3 2 2 4 
    5 1 3 4 2 2 
    结果数量:198
      

  3.   

    public static void main(String[] args){ int[] result = new int[6];
    int[] a = {1, 2, 3, 4, 5, 6};
    show(result, 0, a); }

    private static void show(int[] result, int pos, int[] a) {
    if(pos==a.length) {
    System.out.println(Arrays.toString(result));
    return;
    }
    for(int i=0; i<a.length; i++) {
    if(contains(a[i], result, pos))
    continue;
    else {
    if(pos==2&&a[i]==4) //此句处理“4不在第三位”
    continue;
    if(pos>0&&result[pos-1]==3&&a[i]==5) //此句处理“3和5不相连”
    continue;
    result[pos]=a[i];
    show(result, pos+1, a);
    }
    }
    }

    private static boolean contains(int x, int[] a, int length) {
    for(int i=0; i<length; i++) {
    if(a[i]==x)
    return true;
    }
    return false;
    }
      

  4.   

    转换成字符串问题
    1排除字串“35”“53”
    2charAt(2)不为4