導航:首頁 > 編程大全 > java排列組合工具

java排列組合工具

發布時間:2023-02-21 10:26:01

1. java排列組合演算法如題:用x、y,求出指定長度的所有排列組合。

按照你的要求編寫的求x,y指定長度的所有排列組合的Java程序如下

importjava.util.ArrayList;
importjava.util.List;
publicclassEE{
publicstaticvoidcombination(List<String>list,StringsNumbers,StringsPath,intALen)
{
if(sPath.length()==ALen)
{
list.add(sPath);
return;
}
for(inti=0;i<sNumbers.length();i++)
{

combination(list,sNumbers,sPath+sNumbers.substring(i,i+1),ALen);
}
}
publicstaticvoidmain(String[]args){
List<String>output=newArrayList<String>();
System.out.println("組合");
combination(output,"xy","",5);
for(Strings:output)
System.out.print(s+"");
System.out.println();
System.out.println("共"+output.size()+"個");
}
}

運行結果

組合

xxxxx xxxxy xxxyx xxxyy xxyxx xxyxy xxyyx xxyyy xyxxx xyxxy xyxyx xyxyy xyyxx xyyxy xyyyx xyyyy yxxxx yxxxy yxxyx yxxyy yxyxx yxyxy yxyyx yxyyy yyxxx yyxxy yyxyx yyxyy yyyxx yyyxy yyyyx yyyyy

共32個

2. 用Java編寫程序;求字母A和B的各種排列組合(遞歸思想)

//PS:不太明白函數中Strings的意義,所以忽略了。
importjava.util.ArrayList;
importjava.util.List;


publicclassCompoundString{

privatestaticvoidgenerate(intn,List<String>results){
if(n==1){
return;
}
List<String>resultsTemp_A=newArrayList<String>();
List<String>resultsTemp_B=newArrayList<String>();
for(Stringstr:results){
Stringtemp_A=str+"A";
resultsTemp_A.add(temp_A);
Stringtemp_B=str+"B";
resultsTemp_B.add(temp_B);
}
//此處results引用不能改變,否則任何修改對main函數中的results都無效
results.clear();
results.addAll(resultsTemp_A);
results.addAll(resultsTemp_B);
generate(n-1,results);//recursive
}

publicstaticvoidmain(String[]args){
List<String>results=newArrayList<String>();
results.add("A");
results.add("B");
generate(8,results);
for(Stringstr:results){
.out.println(str);
}
}
}

3. java 定義了5個數字的數組,顯示輸出所有的排列組合

數組排序有專門的API,這里就不說了

獲取1-30的5個隨機數即可

使數組的下標和裡面的值相同,那麼 可以通過 數組[26-當前下標] 是否被去除 來輸出

int a[] = new int[31];

// 賦值,去除

for(int i=1;i<26/2;i++){

if(a[i]!=0 && a[26-i]!=0) 輸出;

4. Java程序如何實現對字元串的排列組合問題

import java.math.BigInteger;
import java.util.*;

public class PermutationGenerator {

private int[] a;
private BigInteger numLeft;
private BigInteger total;
public PermutationGenerator(int n) {
if (n < 1) {
throw new IllegalArgumentException("Min 1");
}
a = new int[n];
total = getFactorial(n);
reset();
}

public void reset() {
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
numLeft = new BigInteger(total.toString());
}

public BigInteger getNumLeft() {
return numLeft;
}

public BigInteger getTotal() {
return total;
}

public boolean hasMore() {
return numLeft.compareTo(BigInteger.ZERO) == 1;
}

private static BigInteger getFactorial(int n) {
BigInteger fact = BigInteger.ONE;
for (int i = n; i > 1; i--) {
fact = fact.multiply(new BigInteger(Integer.toString(i)));
}
return fact;
}

public int[] getNext() {

if (numLeft.equals(total)) {
numLeft = numLeft.subtract(BigInteger.ONE);
return a;
}

int temp;

// Find largest index j with a[j] < a[j+1]

int j = a.length - 2;
while (a[j] > a[j + 1]) {
j--;
}

// Find index k such that a[k] is smallest integer
// greater than a[j] to the right of a[j]

int k = a.length - 1;
while (a[j] > a[k]) {
k--;
}

// Interchange a[j] and a[k]

temp = a[k];
a[k] = a[j];
a[j] = temp;

// Put tail end of permutation after jth position in increasing order

int r = a.length - 1;
int s = j + 1;

while (r > s) {
temp = a[s];
a[s] = a[r];
a[r] = temp;
r--;
s++;
}

numLeft = numLeft.subtract(BigInteger.ONE);
return a;

}
//程序測試入口
public static void main(String[] args) {

int[] indices;
String[] elements = { "a", "b", "c"};
PermutationGenerator x = new PermutationGenerator(elements.length);
StringBuffer permutation;

while (x.hasMore())
{
permutation = new StringBuffer("%");
indices = x.getNext();
for (int i = 0; i < indices.length; i++) {
permutation.append(elements[indices[i]]).append("%");
}
System.out.println(permutation.toString());

}
}

}

先給你一個看看!

5. 用java程序編寫,1234這四個數進行排列組合,

程序已寫出,希望對你有幫助。
import java.util.ArrayList;
import java.util.List;
public class Order {
public static String[] str = {"1","2","3","4"};
public static void main(String[] args){
System.out.println("-------------");
for(String s:str){
System.out.println(s+" , ");
}
System.out.println("-------------");
for(String s:get2byte()){
System.out.print(s+" , ");
}
System.out.println("-------------");
for(String s:get3byte()){
System.out.print(s+" , ");
}
System.out.println("-------------");
for(String s:get4byte()){
System.out.print(s+" , ");
}
}
public static List<String> get2byte(){
List<String> list = new ArrayList<String>();
for(int i=0;i<str.length;i++){
for(int j=0;j<str.length;j++){
list.add(str[i]+str[j]);
}
}
return list;
}
public static List<String> get3byte(){
List<String> list = new ArrayList<String>();
for(int i=0;i<str.length;i++){
for(int j=0;j<str.length;j++){
for(int k=0;k<str.length;k++){
list.add(str[i]+str[j]+str[k]);
}
}
}
return list;
}
public static List<String> get4byte(){
List<String> list = new ArrayList<String>();
for(int i=0;i<str.length;i++){
for(int j=0;j<str.length;j++){
for(int k=0;k<str.length;k++){
for(int h=0;h<str.length;h++){
list.add(str[i]+str[j]+str[k]+str[h]);
}
}
}
}
return list;
}
}

閱讀全文

與java排列組合工具相關的資料

熱點內容
漫一網路培訓學校有哪些 瀏覽:752
酷狗app如何在電腦同步播放 瀏覽:668
雲浮微信群 瀏覽:344
公司文件打不開 瀏覽:267
LOL的設置文件在哪裡 瀏覽:254
線上開庭是什麼app 瀏覽:526
新中大軟體怎麼設置文件夾 瀏覽:807
git刪除文件夾 瀏覽:353
皇室戰爭怎麼升級競技場 瀏覽:447
雅虎統計工具 瀏覽:741
edgepdf文件分類 瀏覽:901
cad為什麼捕捉不到外部參照文件 瀏覽:935
重慶一共有多少個網站 瀏覽:34
k8s配置文件env創建失敗 瀏覽:197
編程序在電腦上叫做什麼 瀏覽:92
qq閱讀可賺 瀏覽:21
怎樣查找web儲存文件圖片 瀏覽:681
人口檔案資料庫包括什麼信息 瀏覽:709
手機有什麼好玩的星戰網路游戲 瀏覽:15
ps怎麼弄成轉曲文件 瀏覽:281

友情鏈接