码迷,mamicode.com
首页 > 编程语言 > 详细

排列的学习(java)

时间:2014-06-28 21:04:53      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:style   blog   java   color   数据      

1.无重复排列

2.有重复排列

3,下一个排列

package 生成排列;


public class Main {
    static int count=0;
    //a中保存原来的排列,lev表示选定第几个数,len是长度
    public static  void swap(int a[],int lev,int i)
    {
        int temp=a[lev];
        a[lev]=a[i];
        a[i]=temp;
        
        
    }
    public static  void swap(char a[],int lev,int i)
    {
        char temp=a[lev];
        a[lev]=a[i];
        a[i]=temp;
        
        
    }

    
    public static void reverse(char c[],int low,int high)
    {
        int l=low;
        int h=high;
        while(l<h)
        {
            swap(c,l,high);
            l++;
            h--;
            
        }
        
        
        
    }
    //利用交换法,获得排列
    public static void  perm(int a[],int lev,int len)
    {
        if(lev==len)
        {
            for(int i=0;i<len;i++)
            {
                System.out.print(a[i]);
                
            }
            
            count++;
        }
        else
        {
            for(int i=lev;i<len;i++)
            {
                swap(a,lev,i);
                perm(a,lev+1,len);
                swap(a,lev,i);
                
            
                
                
                
                
            }
            
            
            
        }
        
        
        
        
    }
    //带有重复数字的排列
    
    public static void perm2(int a[],int lev)
    {
        if(lev==a.length)
        {
            for(int i=0;i<a.length;i++)
            {
                System.out.print(a[i]);
                
            }
            System.out.println();
            count++;
        }
        else
        {
            boolean tf=true;
            for(int j=lev;j<a.length;j++)
            {
                //对于重复的数据不再交换
                for(int k=lev;k<j;k++)
                {
                    if(a[k]==a[j])
                    {
                        tf=false;
                        break;
                    }
                }
                
                if(tf)
                {
                swap(a,lev,j);
                perm2(a, lev+1);
                swap(a,lev,j);
                }
                
                
                
            }
            
            
        }
        
        
        
    }

    
 //下一个排列 STl
    public static boolean nextPerm(char c[]) //输入原始序列
    {
    
        //找到替换点
        int i=c.length -2;
        while(i>=0&&c[i]>=c[i+1])
        {
            i--;
        }
        if(i<0) //说明是最后一个数,翻转后
        {
            reverse(c,0,c.length-1);
            return false;
            
        }
        else
        {
        //否则的话,找到替换点
        //从右边开始找到第一个大于c[i]的位置
        int j=c.length-1;
        while(c[j]<=c[i])
        {
            j--;
            
        }
        //交换两个值
        swap(c,i,j);
        
        reverse(c,i+1,c.length -1);
        return true;
        
        }
        
        
        
        
        
        
        
        
        
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={1,1,1,1,1};
        perm2(a,0);
        System.out.println(count);
        char c[]="1234".toCharArray();
        int count=1;
    /*    while(nextPerm(c))
        {
            
            System.out.println(c);
            count++;
        }
        System.out.println(count);*/

    }
        

}

排列的学习(java),布布扣,bubuko.com

排列的学习(java)

标签:style   blog   java   color   数据      

原文地址:http://www.cnblogs.com/hansongjiang/p/3794534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!