输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻

来源:互联网 发布:友笑网络 编辑:程序博客网 时间:2024/05/17 07:09


package com.csscis.test;import java.util.HashSet;import java.util.Set;/** * @创建日期:Jul 21, 2010 * @版本号:1.0 * @项目名称:sencha */public class Test5 { /**  * @说明:  * 1,2,2,3,4,5的所有组合 4不能排在第三位 ,3和5不能相邻  * @param args  */ private static Set<String> hashSet=new HashSet<String>(); public static void main(String[] args) {  char[] number = new char[] { '1', '2', '2', '3', '4', '5' };  sort(number, 0, number.length - 1);        System.out.println(hashSet.size());        int cols = 10;        for (String s : hashSet) {            System.out.print(s + " ");            if (cols-- == 1) {                System.out.println();                cols = 10;            }        } }  public static void sort(char[] n ,int start,int limit){  if(start==limit){   String result=String.valueOf(n);   if(n[2]=='4'){    return;   }      if(result.contains("35") || result.contains("53")){    return ;   }      hashSet.add(String.valueOf(n));   return;  }  for(int i=start;i<=limit;i++){   Test5.swap(n, start, i);   sort(n,start+1,limit);   Test5.swap(n, start, i);  } }  public static void swap(char[] n,int a,int b){  char temp=n[a];  n[a]=n[b];  n[b]=temp; } }


原创粉丝点击