java組合代碼 java組合編程的原理是什么

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

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

創(chuàng)新互聯(lián)公司長期為近千家客戶提供的網站建設服務,團隊從業(yè)經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網生態(tài)環(huán)境。為橋東企業(yè)提供專業(yè)的成都做網站、成都網站建設,橋東網站改版等技術服務。擁有十載豐富建站經驗和眾多成功案例,為您定制開發(fā)。

復制代碼 代碼如下: import java util Arrays;

//利用二進制算法進行全排列 //count : //count :

public class test { public static void main(String[] args) { long start=System currentTimeMillis(); count (); long end=System currentTimeMillis(); System out println(end start); } private static void count (){ int[] num=new int []{ }; for(int i= ;iMath pow( );i++){ String str=Integer toString(i ); int sz=str length(); for(int j= ;j sz;j++){ str=" "+str; } char[] temp=str toCharArray(); Arrays sort(temp); String gl=new String(temp); if(!gl equals(" ")){ continue; } String result=""; for(int m= ;mstr length();m++){ result+=num[Integer parseInt(str charAt(m)+"")]; } System out println(result); } } public static void count (){ int[] num=new int []{ }; int[] ss=new int []{ }; int[] temp=new int[ ]; while(temp[ ] ){ temp[temp length ]++; for(int i=temp length ;i ;i ){ if(temp[i]== ){ temp[i]= ; temp[i ]++; } } int []tt=temp clone(); Arrays sort(tt); if(!Arrays equals(tt ss)){ continue; } String result=""; for(int i= ;inum length;i++){ result+=num[temp[i]]; } System out println(result); } } }

二 用遞歸的思想來求排列跟組合 代碼量比較大

復制代碼 代碼如下: package practice;

import java util ArrayList; import java util List;

public class Test {

/** * @param args */ public static void main(String[] args) { // TODO Auto generated method stub Object[] tmp={ }; // ArrayListObject[] rs=RandomC(tmp); ArrayListObject[] rs=cmn(tmp ); for(int i= ;irs size();i++) { // System out print(i+"="); for(int j= ;jrs get(i) length;j++) { System out print(rs get(i)[j]+" "); } System out println(); } }

// 求一個數(shù)組的任意組合 static ArrayListObject[] RandomC(Object[] source) { ArrayListObject[] result=new ArrayListObject[](); if(source length== ) { result add(source); } else { Object[] psource=new Object[source length ]; for(int i= ;ipsource length;i++) { psource[i]=source[i]; } result=RandomC(psource); int len=result size();//fn組合的長度 result add((new Object[]{source[source length ]})); for(int i= ;ilen;i++) { Object[] tmp=new Object[result get(i) length+ ]; for(int j= ;jtmp length ;j++) { tmp[j]=result get(i)[j]; } tmp[tmp length ]=source[source length ]; result add(tmp); } } return result; } static ArrayListObject[] cmn(Object[] source int n) { ArrayListObject[] result=new ArrayListObject[](); if(n== ) { for(int i= ;isource length;i++) { result add(new Object[]{source[i]}); } } else if(source length==n) { result add(source); } else { Object[] psource=new Object[source length ]; for(int i= ;ipsource length;i++) { psource[i]=source[i]; } result=cmn(psource n); ArrayListObject[] tmp=cmn(psource n ); for(int i= ;itmp size();i++) { Object[] rs=new Object[n]; for(int j= ;jn ;j++) { rs[j]=tmp get(i)[j]; } rs[n ]=source[source length ]; result add(rs); } } return result; }

}

三 利用動態(tài)規(guī)劃的思想求排列和組合

復制代碼 代碼如下: package Acm; //強大的求組合數(shù) public class MainApp { public static void main(String[] args) { int[] num=new int[]{ }; String str=""; //求 個數(shù)的組合個數(shù) // count( str num ); // 求 n個數(shù)的組合個數(shù) count ( str num); }

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

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

下面是求排列

復制代碼 代碼如下: lishixinzhi/Article/program/Java/JSP/201311/20148

輸入任意字符序列,輸出所有兩位數(shù)的排列組合JAVA代碼?

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// 創(chuàng)建Scanner對象,用于獲取用戶輸入

Scanner scanner = new Scanner(System.in);

System.out.print("請輸入任意字符序列:");

// 獲取用戶輸入的字符序列

String str = scanner.nextLine();

// 循環(huán)遍歷字符序列中的每個字符

for (int i = 0; i str.length(); i++) {

// 獲取字符序列中的第i個字符

char c1 = str.charAt(i);

// 循環(huán)遍歷字符序列中的每個字符

for (int j = 0; j str.length(); j++) {

// 獲取字符序列中的第j個字符

char c2 = str.charAt(j);

// 如果第i個字符不等于第j個字符,則輸出它們的排列

if (i != j) {

System.out.println(c1 + "" + c2);

}

}

}

}

}

求java實現(xiàn)String list[] = { "1", "2", "3" }; 的排列組合代碼

對于這個問題,我首先需要糾正一下樓主的措辭,這是個組合問題,跟排列無關,用排列組合亦不恰當。下面說下我的想法

元素不能重復,首先應該去掉相同的元素,最好的辦法是用set來實現(xiàn)。參考api

Arrays.asList

set.addAll

其實呢,這個是一個遞歸的過程,考慮下面情況

對于數(shù)組

{“1”},它的組合數(shù)就是{“1”}。

如果再加上一個元素“2“到上面的數(shù)組中,那么,如果這個”2“不用,實質上跟{"1"}的情況是一樣的,這與不能重復相矛盾,所以”2“一定要用,就是在"1"中再加上”2“;于是我們得到

對于數(shù)組{”1“,”2“}它的組合數(shù)是{”1“}

再加入一個{”2“}。也許你也考慮到另外一種情況,即”2“也是它的一個組合數(shù),我們考慮丟了,為什么呢,因為在{”1“}中實質上還有一個稱為空的集合。這樣的話,重新整理一下:

1.對于list

=

{"1"},它的組合包括

{"1"},以及

empty.

2.對于list={"1","2"},它的組合包括{”1“,”2“}(在{”1“}中加了”2“),{”2“}(在empty中加入”2“),也許你還會講還應該包括{”1“},但是這個{”1“}我們已經在第1步就已經算出來了,不用再考慮了。

按照這樣的規(guī)則進行下去,你會發(fā)現(xiàn)這樣就把所有的組合數(shù)遍歷出來了。要具體的代碼就等會兒,我現(xiàn)在有事。

網站名稱:java組合代碼 java組合編程的原理是什么
文章源于:http://muchs.cn/article4/ddcijoe.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、網站導航、手機網站建設網站內鏈、標簽優(yōu)化小程序開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

手機網站建設