全排列java代碼 java編程排序

java 全排列算法;

= =~思路什么的...用遞歸吧:

創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè)|成都網(wǎng)站維護(hù)|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋白烏魚等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身定制品質(zhì)網(wǎng)站。

package mon_11;

import java.util.HashSet;

public class ArrangeAll {

private static HashSetString set = new HashSetString();

public static void arrangeAll(String s) {

put(new StringBuilder(s), new StringBuilder());

}

static void put(StringBuilder s1, StringBuilder s2) {

if (s1.length() == 0)set.add(s2.toString());

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

put(new StringBuilder(s1).deleteCharAt(i),new StringBuilder(s2).append(s1.charAt(i)));

}

}

public static void main(String[] args) {

arrangeAll("abcd");

System.out.println(set);

}

}

----

輸出:

[dcab, acdb, acbd, bcda, bdca, bdac, dbca, bacd, cabd, cdba, cdab, badc, dabc, cadb, dbac, bcad, dacb, cbda, cbad, adbc, adcb, abcd, abdc, dcba]

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

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

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

Scanner scanner = new Scanner(System.in);

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

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

String str = scanner.nextLine();

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

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

// 獲取字符序列中的第i個(gè)字符

char c1 = str.charAt(i);

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

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

// 獲取字符序列中的第j個(gè)字符

char c2 = str.charAt(j);

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

if (i != j) {

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

}

}

}

}

}

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

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

復(fù)制代碼 代碼如下: import java util Arrays;

//利用二進(jìn)制算法進(jìn)行全排列 //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); } } }

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

復(fù)制代碼 代碼如下: 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(); } }

// 求一個(gè)數(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組合的長(zhǎng)度 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; }

}

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

復(fù)制代碼 代碼如下: package Acm; //強(qiáng)大的求組合數(shù) public class MainApp { public static void main(String[] args) { int[] num=new int[]{ }; String str=""; //求 個(gè)數(shù)的組合個(gè)數(shù) // count( str num ); // 求 n個(gè)數(shù)的組合個(gè)數(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); } }

下面是求排列

復(fù)制代碼 代碼如下: lishixinzhi/Article/program/Java/JSP/201311/20148

分享名稱:全排列java代碼 java編程排序
當(dāng)前URL:http://www.muchs.cn/article16/ddijpgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、云服務(wù)器、微信公眾號(hào)、網(wǎng)站改版、小程序開發(fā)

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)