给定一个数组,求出数组元素的排列和组合——Java实现

来源:互联网 发布:军娘捏脸数据 编辑:程序博客网 时间:2024/06/06 02:59

1. 思路

组合数C(n,m)和全排列A(n,n)可以通过递归的方式,直接实现。

而A(n,m)则可以通过组合数和全排列间接求出A(n,m)=C(n,m)*A(m,m),即对得到的组合数中的每个元素进行全排列


2. Java实现


package com.zfy.test3;import java.util.*;public class PC {public static void main(String[] args){char[] str={'A','B','C','D','E'};ArrayList<Character> t=new ArrayList<Character>();ArrayList<ArrayList<Character>> res=new ArrayList<ArrayList<Character>>();//求组合数Combination(str,5,3,t,res);System.out.println(res);//求全排列System.out.println(Permutation(str,5,3));}//全排列方法1public static void permulation(int[] list, int start, int length) {        int i;        if (start == length) {            for (i = 0; i < length; i++)                System.out.print(list[i] + " ");            System.out.println();        } else {            for (i = start; i < length; i++) {                swap(list, start, i);                permulation(list, start + 1, length);                swap(list, start, i);            }        }    }    public static void swap(int[] list, int start, int i) {        int temp;        temp = list[start];        list[start] = list[i];        list[i] = temp;    }    //全排列方法2    public static ArrayList<ArrayList<Character>> Permutation(char[] A,int n){if(n==1){ArrayList<Character> t=new ArrayList<Character>();ArrayList<ArrayList<Character>> temp=new ArrayList<ArrayList<Character>>();t.add(A[0]);temp.add(t);return temp;}else{ArrayList<ArrayList<Character>> temp=Permutation(A,n-1);ArrayList<ArrayList<Character>> res=new ArrayList<ArrayList<Character>>();for(int i=0;i<temp.size();i++){for(int j=0;j<n;j++){ArrayList<Character> t=new ArrayList<Character>(temp.get(i));if(j<n-1){t.add(j,A[n-1]);res.add(t);}else{t.add(A[n-1]);res.add(t);}}}return res;}}    //指定个数元素的排列    public static ArrayList<ArrayList<Character>> Permutation(char[] A,int n,int m)    {    ArrayList<ArrayList<Character>> temp=new ArrayList<ArrayList<Character>>();    ArrayList<Character> t=new ArrayList<Character>();    Combination(A,n,m,t,temp);        ArrayList<ArrayList<Character>> res=new ArrayList<ArrayList<Character>>();    for(int i=0;i<temp.size();i++)    {        char[] tc=new char[temp.get(i).size()];    for(int j=0;j<tc.length;j++)    {    tc[j]=temp.get(i).get(j);    }    res.addAll(Permutation(tc,tc.length));    }    return res;    }//组合数,通过参数返回结果public static void Combination(char[] A,int n,int m,ArrayList<Character> t,ArrayList<ArrayList<Character>> res){if(m==0){ArrayList<Character> temp=new ArrayList<Character>(t);res.add(temp);}else{//for(int i=n-1;i>=m-1;i--)//{//t.add(A[i]);//Combination(A,i,m-1,t,res);//t.remove(t.size()-1);//}for(int i=A.length-n;i<=A.length-m;i++){t.add(A[i]);Combination(A,A.length-i-1,m-1,t,res);t.remove(t.size()-1);}}}//组合数,通过返回值返回结果public static ArrayList<ArrayList<Character>>  Combination(char[] A,int n,int m,ArrayList<Character> t){ArrayList<ArrayList<Character>> res=new ArrayList<ArrayList<Character>>();if(m==0){ArrayList<Character> temp=new ArrayList<Character>(t);res.add(temp);return res;}else{//for(int i=n-1;i>=m-1;i--)//{//t.add(A[i]);//Combination(A,i,m-1,t,res);//t.remove(t.size()-1);//}for(int i=A.length-n;i<=A.length-m;i++){t.add(A[i]);res.addAll(Combination(A,A.length-i-1,m-1,t));t.remove(t.size()-1);}return res;}}}


0 0
原创粉丝点击