对 {1, 2, 2, 3, 4, 5} 进行无重复的全排列,要求 4 不在第三位,且 3,5 不相连

本文介绍了一种针对特定约束条件的全排列算法实现,该算法能够处理包含重复元素的序列,并确保排列结果满足特定条件:4不在第三位,且3与5不相邻。通过递归方法实现了对{1,2,2,3,4,5}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        今天在看社区的帖子的时候发现有个朋友提了这样的一个问题,想要做个一个排序的算法,要求对 {1, 2, 2, 3, 4, 5} 进行无重复的全排列,要求 4 不在第三位,且 3,5 不相连,我也调了个程序,拿来大家一起分享。

        

  1. package sort;
  2. import java.util.ArrayList;
  3. /**
  4.  * 对 {1, 2, 2, 3, 4, 5} 进行无重复的全排列,要求 4 不在第三位,且 3,5 不相连。
  5.  */
  6. public class AllSort
  7. {
  8.     static ArrayList<int[]> results = new ArrayList<int[]>();
  9.     public static void main(String[] args)
  10.     {
  11.         int[] ints = { 122345 };
  12.         roll(ints, 0);
  13.         for (int[] result : results)
  14.         {
  15.             printArr(result);
  16.         }
  17.         System.out.println("结果数量:" + results.size());
  18.     }
  19.     /**
  20.      * 递归构造全排列假设要排列的数组已经从小到大排好序。
  21.      * @param ints   要排列的数组
  22.      * @param pointer    进行排列的起始位置
  23.      */
  24.     private static void roll(int[] ints, int pointer)
  25.     {
  26.         if (ints.length < 2)
  27.         {
  28.             return;
  29.         }
  30.         if (pointer == ints.length - 1)
  31.         {
  32.             saveResult(ints);
  33.             return;
  34.         }
  35.         int[] cloned;
  36.         for (int i = pointer; i < ints.length; i++)
  37.         {
  38.             cloned = ints.clone();
  39.             int t = cloned[i];
  40.             cloned[i] = cloned[pointer];
  41.             cloned[pointer] = t;
  42.             roll(cloned, pointer + 1);
  43.         }
  44.     }
  45.     /**
  46.      * 判断数组是否与现有结果重复且满足条件,如果是则存入结果。
  47.      * @param ints  要保存的数组
  48.      */
  49.     private static void saveResult(int[] ints)
  50.     {
  51.         boolean exists = false;
  52.         for (int[] result : results)
  53.         {
  54.             if (same(result, ints))
  55.             {
  56.                 exists = true;
  57.                 break;
  58.             }
  59.         }
  60.         if (!exists && available(ints))
  61.         {
  62.             results.add(ints);
  63.         }
  64.     }
  65.     /**
  66.      * 比较两个数组是否完全相同
  67.      * @param a       一个数组
  68.      * @param b       另一个数组
  69.      * @return 如果数组中每个元素都相同,则返回 true。
  70.      */
  71.     private static boolean same(int[] a, int[] b)
  72.     {
  73.         boolean same = true;
  74.         for (int i = 0; i < a.length; i++)
  75.         {
  76.             if (a[i] != b[i])
  77.             {
  78.                 same = false;
  79.                 break;
  80.             }
  81.         }
  82.         return same;
  83.     }
  84.     /**
  85.      * 输出数组
  86.      * @param ints          要输出的数组
  87.      */
  88.     private static void printArr(int[] ints)
  89.     {
  90.         for (int n : ints)
  91.         {
  92.             System.out.print(n + " ");
  93.         }
  94.         System.out.println();
  95.     }
  96.     /**
  97.      * 检查数组是否符合要求
  98.      * @param ints      要检查的数组
  99.      * @return 如果 4 在第三位,或者 3 和 5 相连,则返回 false。
  100.      */
  101.     private static boolean available(int[] ints)
  102.     {
  103.         return ints[2] != 4
  104.                 && (Math.abs(indexOf(ints, 3) - indexOf(ints, 5)) > 1);
  105.     }
  106.     /**
  107.      * 查找指定数字在数组中的位置
  108.      * @param ints         指定数组
  109.      * @param n         指定数字
  110.      * @return 指定数字在数组中的位置。如果找不到,则返回 -1。
  111.      */
  112.     private static int indexOf(int[] ints, int n)
  113.     {
  114.         for (int i = 0; i < ints.length; i++)
  115.         {
  116.             if (ints[i] == n)
  117.             {
  118.                 return i;
  119.             }
  120.         }
  121.         return -1;
  122.     }
  123. }

          程序运行的结果是:

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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 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
        程序主要的思路是,先进行全部的排列,然后在使用过滤条件把不满足条件的去除……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值