欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于各種排列組合java算法實(shí)現(xiàn)方法

 更新時(shí)間:2013年06月30日 15:17:39   作者:  
這篇文章介紹了幾種用JAVA實(shí)現(xiàn)的排列組合算法,有需要的朋友可以參考一下

一.利用二進(jìn)制狀態(tài)法求排列組合,此種方法比較容易懂,但是運(yùn)行效率不高,小數(shù)據(jù)排列組合可以使用

復(fù)制代碼 代碼如下:

import java.util.Arrays;

//利用二進(jìn)制算法進(jìn)行全排列
//count1:170187
//count2:291656

public class test {
    public static void main(String[] args) {
        long start=System.currentTimeMillis();
        count2();
        long end=System.currentTimeMillis();
        System.out.println(end-start);
    }
    private static void count2(){
        int[] num=new int []{1,2,3,4,5,6,7,8,9};
        for(int i=1;i<Math.pow(9, 9);i++){
            String str=Integer.toString(i,9);
            int sz=str.length();
            for(int j=0;j<9-sz;j++){
                str="0"+str;
            }
            char[] temp=str.toCharArray();
            Arrays.sort(temp);
            String gl=new String(temp);
            if(!gl.equals("012345678")){
                continue;
            }
            String result="";
            for(int m=0;m<str.length();m++){
                result+=num[Integer.parseInt(str.charAt(m)+"")];
            }
            System.out.println(result);
        }
    }
    public static void count1(){
        int[] num=new int []{1,2,3,4,5,6,7,8,9};
        int[] ss=new int []{0,1,2,3,4,5,6,7,8};
        int[] temp=new int[9];
        while(temp[0]<9){
            temp[temp.length-1]++;
            for(int i=temp.length-1;i>0;i--){
                if(temp[i]==9){
                    temp[i]=0;
                    temp[i-1]++;
                }
            }
            int []tt=temp.clone();
            Arrays.sort(tt);
            if(!Arrays.equals(tt,ss)){
                continue;
            }
            String result="";
            for(int i=0;i<num.length;i++){
                result+=num[temp[i]];
            }
            System.out.println(result);

        }
    }
}


二.用遞歸的思想來求排列跟組合,代碼量比較大
復(fù)制代碼 代碼如下:

package practice;

import java.util.ArrayList;
import java.util.List;


public class Test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Object[] tmp={1,2,3,4,5,6};
//        ArrayList<Object[]> rs=RandomC(tmp);
        ArrayList<Object[]> rs=cmn(tmp,3);
        for(int i=0;i<rs.size();i++)
        {
//            System.out.print(i+"=");
            for(int j=0;j<rs.get(i).length;j++)
            {
                System.out.print(rs.get(i)[j]+",");
            }
            System.out.println();

        }
    }

   
    // 求一個數(shù)組的任意組合
    static ArrayList<Object[]> RandomC(Object[] source)
    {
        ArrayList<Object[]> result=new ArrayList<Object[]>();
        if(source.length==1)
        {
            result.add(source);       
        }
        else
        {
            Object[] psource=new Object[source.length-1];
            for(int i=0;i<psource.length;i++)
            {
                psource[i]=source[i];
            }
            result=RandomC(psource);
            int len=result.size();//fn組合的長度
            result.add((new Object[]{source[source.length-1]}));
            for(int i=0;i<len;i++)
            {
                Object[] tmp=new Object[result.get(i).length+1];
                for(int j=0;j<tmp.length-1;j++)
                {
                    tmp[j]=result.get(i)[j];
                }
                tmp[tmp.length-1]=source[source.length-1];
                result.add(tmp);
            }

        }
        return result;
    }

    static ArrayList<Object[]> cmn(Object[] source,int n)
    {
        ArrayList<Object[]> result=new ArrayList<Object[]>();
        if(n==1)
        {
            for(int i=0;i<source.length;i++)
            {
                result.add(new Object[]{source[i]});

            }
        }
        else if(source.length==n)
        {
            result.add(source);
        }
        else
        {
            Object[] psource=new Object[source.length-1];
            for(int i=0;i<psource.length;i++)
            {
                psource[i]=source[i];
            }
            result=cmn(psource,n);
            ArrayList<Object[]> tmp=cmn(psource,n-1);
            for(int i=0;i<tmp.size();i++)
            {
                Object[] rs=new Object[n];
                for(int j=0;j<n-1;j++)
                {
                    rs[j]=tmp.get(i)[j];
                }
                rs[n-1]=source[source.length-1];
                result.add(rs);
            }
        }
        return result;
    }

}


三.利用動態(tài)規(guī)劃的思想求排列和組合
復(fù)制代碼 代碼如下:

package Acm;
//強(qiáng)大的求組合數(shù)
public class MainApp {
    public static void main(String[] args) {
        int[] num=new int[]{1,2,3,4,5};
        String str="";
        //求3個數(shù)的組合個數(shù)
//        count(0,str,num,3);
//        求1-n個數(shù)的組合個數(shù)
        count1(0,str,num);
    }

    private static void count1(int i, String str, int[] num) {
        if(i==num.length){
            System.out.println(str);
            return;
        }
        count1(i+1,str,num);
        count1(i+1,str+num[i]+",",num);
    }

    private static void count(int i, String str, int[] num,int n) {
        if(n==0){
            System.out.println(str);
            return;
        }
        if(i==num.length){
            return;
        }
        count(i+1,str+num[i]+",",num,n-1);
        count(i+1,str,num,n);
    }
}


下面是求排列
復(fù)制代碼 代碼如下:

package Acm;
//求排列,求各種排列或組合后排列
import java.util.Arrays;
import java.util.Scanner;

public class Demo19 {
    private static boolean f[];
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int sz=sc.nextInt();
        for(int i=0;i<sz;i++){
            int sum=sc.nextInt();
            f=new boolean[sum];
            Arrays.fill(f, true);
            int[] num=new int[sum];
            for(int j=0;j<sum;j++){
                num[j]=j+1;
            }
            int nn=sc.nextInt();
            String str="";
            count(num,str,nn);
        }
    }
    /**
     *
     * @param num 表示要排列的數(shù)組
     * @param str 以排列好的字符串
     * @param nn  剩下需要排列的個數(shù),如果需要全排列,則nn為數(shù)組長度
     */
    private static void count(int[] num, String str, int nn) {
        if(nn==0){
            System.out.println(str);
            return;
        }
        for(int i=0;i<num.length;i++){
            if(!f[i]){
                continue;
            }
            f[i]=false;
            count(num,str+num[i],nn-1);
            f[i]=true;
        }
    }
}

相關(guān)文章

  • Java如何基于ProcessBuilder類調(diào)用外部程序

    Java如何基于ProcessBuilder類調(diào)用外部程序

    這篇文章主要介紹了Java如何基于ProcessBuilder類調(diào)用外部程序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • java反射之獲取類的信息方法(推薦)

    java反射之獲取類的信息方法(推薦)

    下面小編就為大家?guī)硪黄猨ava反射之獲取類的信息方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 如何基于JWT實(shí)現(xiàn)接口的授權(quán)訪問詳解

    如何基于JWT實(shí)現(xiàn)接口的授權(quán)訪問詳解

    授權(quán)是最常見的JWT使用場景,下面這篇文章主要給大家介紹了關(guān)于如何基于JWT實(shí)現(xiàn)接口的授權(quán)訪問的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • Struts2實(shí)現(xiàn)自定義攔截器的三種方式詳解

    Struts2實(shí)現(xiàn)自定義攔截器的三種方式詳解

    這篇文章主要介紹了Struts2實(shí)現(xiàn)自定義攔截器的三種方式詳解,一些與系統(tǒng)邏輯相關(guān)的通用功能如權(quán)限的控制和用戶登錄控制等,需要通過自定義攔截器實(shí)現(xiàn),本節(jié)將詳細(xì)講解如何自定義攔截器,需要的朋友可以參考下
    2023-07-07
  • druid?return行為方法源碼示例解析

    druid?return行為方法源碼示例解析

    這篇文章主要為大家介紹了druid?return行為源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 基于RabbitMQ幾種Exchange 模式詳解

    基于RabbitMQ幾種Exchange 模式詳解

    下面小編就為大家?guī)硪黄赗abbitMQ幾種Exchange 模式詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Java中jqGrid 學(xué)習(xí)筆記整理——進(jìn)階篇(二)

    Java中jqGrid 學(xué)習(xí)筆記整理——進(jìn)階篇(二)

    這篇文章主要介紹了Java中jqGrid 學(xué)習(xí)筆記整理——進(jìn)階篇(二)的相關(guān)資料,需要的朋友可以參考下
    2016-04-04
  • 解決Error:(1,?1)?java:?非法字符:?'\ufeff'問題

    解決Error:(1,?1)?java:?非法字符:?'\ufeff'問題

    這篇文章主要介紹了解決Error:(1,?1)?java:?非法字符:?'\ufeff'問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java設(shè)計(jì)模式之策略模式詳解和示例

    Java設(shè)計(jì)模式之策略模式詳解和示例

    這篇文章主要介紹了Java設(shè)計(jì)模式之策略模式詳解和示例,策略模式就是一種行為可能會因?yàn)椴煌倪壿嬙斐啥鄠€算法,比如人吃飯,美國人吃飯用刀叉,中國吃飯用筷子,都是吃飯的行為但是使用的工具(算法)不一樣,需要的朋友可以參考下
    2024-01-01
  • java如何實(shí)現(xiàn)數(shù)位分離

    java如何實(shí)現(xiàn)數(shù)位分離

    這篇文章主要介紹了java如何實(shí)現(xiàn)數(shù)位分離,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論