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

来源:互联网 发布:紫鸟数据魔方公司 编辑:程序博客网 时间:2024/05/16 20:39
输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
原始题目:http://topic.csdn.net/u/20080515/17/a3bba247-4970-45c2-bd94-1478a39ef4bd.html

代码:
package niko7.csdn;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 * 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
 * 
 * 转载、引用必须保留此段信息。
 * 
@author niko7,(顾法华,杭州)
 * @email niko7@163.com
 *
 
*/

public class MyTest
{
    
public static void main(String[] args)
    
{
        
int[] nums = new int[]{1,2,2,3,4,5};
        
int len = nums.length;
        Set set 
= new TreeSet();
        
int[] indexs = new int[]{0,0,0,0,0,0};
        
for(indexs[0]=0; indexs[0]<len; indexs[0]++)
            
for(indexs[1]=0; indexs[1]<len; indexs[1]++)
                
for(indexs[2]=0; indexs[2]<len; indexs[2]++)
                    
for(indexs[3]=0; indexs[3]<len; indexs[3]++)
                        
for(indexs[4]=0; indexs[4]<len; indexs[4]++)
                            
for(indexs[5]=0; indexs[5]<len; indexs[5]++)
                            
{
                                
//同一个字符重复出现的跳过
                                
//两个2不是同一个字符
                                boolean skip = false;
                                a:
                                
for(int i=0;i<len;i++)
                                    
for(int j=i+1;j<len;j++)
                                    
{
                                        
if(indexs[i]==indexs[j])
                                        
{
                                            skip 
= true;
                                            
break a;
                                        }

                                    }

                                
if(skip)
                                
{
                                    
continue;
                                }

                                
                                
//4不能在第三位
                                if(4==nums[indexs[2]])
                                
{
                                    
continue;
                                }

                                
                                
//3和5不能相邻
                                boolean ok = true;
                                
int[] tmpResult = new int[]{nums[indexs[0]],nums[indexs[1]],nums[indexs[2]],nums[indexs[3]],nums[indexs[4]],nums[indexs[5]]};
                                
for(int i=0; i<len-1; i++)
                                
{
                                    
if(2==Math.abs(tmpResult[i]-tmpResult[i+1]) && 8==(tmpResult[i]+tmpResult[i+1]))
                                    
{
                                        ok 
= false;
                                        
break;
                                    }

                                }

                                
                                
if(ok)
                                
{
                                    
//自动去重,其实这是针对两个2的
                                    set.add(""+tmpResult[0]+tmpResult[1]+tmpResult[2]+tmpResult[3]+tmpResult[4]+tmpResult[5]);
                                }

                            }

        System.out.println(
"共有 "+set.size()+" 个");
        Iterator iter 
= set.iterator();
        
int no = 1;
        
while(iter.hasNext())
        
{
            
if(no%10==1)
            
{
                System.out.println();
                System.out.print(no);
            }

            no
++;
            System.out.print(
"" + iter.next());            
        }
        
    }
    
}

结果:
共有 
198 个

1    122345    122543    123245    123254    123425    123452    125234    125243    125423    125432
11    132245    132254    132425    132452    132524    132542    142325    142523    143225    143252
21    145223    145232    152234    152243    152324    152342    152423    152432    212345    212543
31    213245    213254    213425    213452    215234    215243    215423    215432    221345    221543
41    223145    223154    223415    223451    225134    225143    225413    225431    231245    231254
51    231425    231452    231524    231542    232145    232154    232415    232451    232514    232541
61    241325    241523    242315    242513    243125    243152    243215    243251    245123    245132
71    245213    245231    251234    251243    251324    251342    251423    251432    252134    252143
81    252314    252341    252413    252431    312245    312254    312425    312452    312524    312542
91    315224    315242    315422    321245    321254    321425    321452    321524    321542    322145
101    322154    322415    322451    322514    322541    325124    325142    325214    325241    325412
111    325421    341225    341252    341522    342125    342152    342215    342251    342512    342521
121    345122    345212    345221    412325    412523    413225    413252    415223    415232    421325
131    421523    422315    422513    423125    423152    423215    423251    425123    425132    425213
141    425231    431225    431252    431522    432125    432152    432215    432251    432512    432521
151    451223    451232    451322    452123    452132    452213    452231    452312    452321    512234
161    512243    512324    512342    512423    512432    513224    513242    513422    521234    521243
171    521324    521342    521423    521432    522134    522143    522314    522341    522413    522431
181    523124    523142    523214    523241    523412    523421    541223    541232    541322    542123
191    542132    542213    542231    542312    542321    543122    543212    543221
原创粉丝点击