2013年用友校招编程题:用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列

用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连 

 

这道笔试题相当的出名,我也有幸遇到了。确切的说这是一道算法题,像我这种离散,数据结构都半吊子的人实在拿它没撤,回来小发愤了一下,找到了多个不同的实现算法

第一个:

Java代码   收藏代码
  1. package com.audition;  
  2. //排序组合算法  
  3. public class PermutationAlgo {  
  4.   private int count = 0;  
  5.     
  6.   public void calculate(){  
  7.     String eleStr = "122345";  
  8.     depthSearch(eleStr, "");  
  9.     System.out.println("符合条件的总结果数为:"+count+"条");  
  10.   }  
  11.     
  12.   /** 
  13.    * @param eleStr - 待分配字符组成的串 
  14.    * @param rstStr - 已分配字符组成的串 
  15.    */  
  16.   public void depthSearch(String eleStr, String rstStr) {  
  17.     if (eleStr.length() == 0) {  
  18.       count++;  
  19.       System.out.println(rstStr);  
  20.       return;  
  21.     }  
  22.     for (int i = 0; i < eleStr.length(); i++) {  
  23.       String currEle = eleStr.substring(i, i + 1); //取出当前位的值  
  24.       if (rstStr.length() == 2 && "4".equals(currEle)) continue//剪掉第三位为4的分支  
  25.       if (rstStr.endsWith("3") && "5".equals(currEle)) continue//剪掉"35"相连的分支  
  26.       if (rstStr.endsWith("5") && "3".equals(currEle)) continue//剪掉"53"相连的分支  
  27.       if (eleStr.substring(0, i).indexOf(currEle) != -1continue//剪掉同一位上字符重复的分支(此题即剪掉重复的2)  
  28.       depthSearch(eleStr.substring(0, i) + eleStr.substring(i + 1), rstStr + currEle); //用剩余的合法串继续递归  
  29.     }  
  30.   }  
  31.     
  32.   public static void main(String[] args) {  
  33.     new PermutationAlgo().calculate();  
  34.   }  
  35. }  

 第二个

Java代码   收藏代码
  1. package com.audition;  
  2.   
  3. public class PermutationAlgo2 {  
  4.     private static int count = 0;  
  5.         public static void main(String[] args) {  
  6.             // 本题的思路是先用 1,3,4,5 进行全排列  
  7.             // 后边再将另外两个 2 往里插入,并判断是否符合要求  
  8.             int[] arr = { 1345 };  
  9.             printCom(arr, 0);  
  10.             System.out.printf("符合要求的排列共有 %d 种", count);  
  11.         }  
  12.       
  13.         public static void printCom(int[] arr, int index) {  
  14.             if (index == arr.length - 1) {  
  15.                 insertTwo(arr, 0);  
  16.                 return;  
  17.             }  
  18.             printCom(arr, index + 1);  
  19.             for (int i = index + 1; i < arr.length; i++) {  
  20.                 arr[index] ^= arr[i];  
  21.                 arr[i] ^= arr[index];  
  22.                 arr[index] ^= arr[i];  
  23.                 printCom(arr, index + 1);  
  24.                 arr[index] ^= arr[i];  
  25.                 arr[i] ^= arr[index];  
  26.                 arr[index] ^= arr[i];  
  27.             }  
  28.         }  
  29.       
  30.         // 将 2 在符合要求的情况下插入数组  
  31.         private static void insertTwo(int[] arr, int index) {  
  32.             if (index == arr.length + 1)  
  33.                 return;  
  34.             for (int i = index + 1; i < arr.length + 2; i++) {  
  35.                 int[] tar = new int[arr.length + 2];  
  36.                 tar[i] = 2;  
  37.                 tar[index] = 2;  
  38.                 innerInsert(arr, tar);  
  39.             }  
  40.             insertTwo(arr, index + 1);  
  41.         }  
  42.       
  43.         private static void innerInsert(int[] arr, int[] tar) {  
  44.             int index = 0;  
  45.             int indexArr = 0;  
  46.             while (index < tar.length) {  
  47.                 if (tar[index] == 0) {  
  48.                     if ((index > 0 && tar[index - 1] + arr[indexArr] == 8)  
  49.                             || (index == 2 && arr[indexArr] == 4))  
  50.                         return;  
  51.                     tar[index] = arr[indexArr++];  
  52.                 }  
  53.                 index++;  
  54.             }  
  55.             count++;  
  56.             System.out.printf("[%d]", count);  
  57.             for (int i = 0; i < tar.length; i++) {  
  58.                 System.out.print(tar[i] + " ");  
  59.             }  
  60.             if(count % 5 == 0){  
  61.                 System.out.println();  
  62.             }  
  63.         }  
  64.       
  65. }  

 第三个

Java代码   收藏代码
  1. package com.audition;  
  2.   
  3. /** 
  4.  * 用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5" 
  5.  * 不能相连。 
  6.  *  
  7.  *  
  8.  * @author Administrator 
  9.  *  
  10.  */  
  11. public class PermutationAlgo3 {  
  12.     private int[] numbers = new int[] { 122345 };  
  13.     public int n;  
  14.     private String lastResult = "";  
  15.   
  16.     private boolean validate(String s) {  
  17.         if (s.compareTo(lastResult) <= 0)  
  18.             return false;  
  19.         if (s.charAt(2) == '4')  
  20.             return false;  
  21.         if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0)  
  22.             return false;  
  23.         return true;  
  24.     }  
  25.   
  26.     public void list(String index, String result) {  
  27.         for (int i = 0; i < numbers.length; i++) {  
  28.             if (index.indexOf(i + 48) < 0) {  
  29.                 String s = result + String.valueOf(numbers[i]);  
  30.                 if (s.length() == numbers.length) {  
  31.                     if (validate(s)) {  
  32.                         System.out.println(s);  
  33.                         lastResult = s;  
  34.                         n++;  
  35.                     }  
  36.                     break;  
  37.                 }  
  38.                 list(index + String.valueOf(i), s);  
  39.             }  
  40.         }  
  41.     }  
  42.       
  43.   
  44.     public static void main(String[] args) {  
  45.         PermutationAlgo3 algo3 = new PermutationAlgo3();  
  46.         algo3.list("""");  
  47.         System.out.println("总数:" + algo3.n);  
  48.     }  
  49. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值